-4

当用户上传图片或文档时。图片或文档未存储在上传文件夹中,但剩余字段已成功插入数据库。我想将它存储在上传文件夹中。

4

2 回答 2

0

请参见

if(isset($_REQUEST['submit'])){  
 $file=$_FILES['myfile']['name'];
 $path="PATH YOUR DIRECTORY";

 if(move_uploaded_file($_FILES['myfile']['tmp_name'],$path.$file))
 {
   echo "FILE UPLOAD SUCCESSFULLY";
 }
 else{
   echo "FAILED";
 }

}

在您的 <form> 属性中使用 enctype ,否则您的文件不会上传到您的服务器中。

enctype="multipart/form-data"

HTML 代码

<form method="post" enctype="multipart/form-data">
File: <input type="file" name="myfile" />
<input type="submit" name="submit" value="Upload" />
</form>

更多信息访问

http://www.learnphp.in/mypage_6_File-uploading-with-PHP.html

于 2013-08-13T05:15:39.580 回答
0
public function uploadImage()
{
    //define a maxim size for the uploaded images in Kb
     define ("MAX_SIZE","1000"); 


    //checks if the form has been submitted
    if(isset($_POST['picture'])) 
    {
        //reads the name of the file the user submitted for uploading
        $image=$_FILES['file']['name'];
        if ($_FILES["file"]["error"] > 0)
        {
            $this->errorMsg = $_FILES["file"]["error"] . "<br />";
            $this->errors = 1;
         }

        //if it is not empty

        //get the original name of the file from the clients machine
        $filename = stripslashes($_FILES['file']['name']);

        //get the extension of the file in a lower case format
        $extension = $this->getExtension($filename);

        $extension = strtolower($extension);
        //if it is not a known extension, we 
        //will suppose it is an error and will not  upload the file,  
        //otherwise we will do more tests
        if (($extension != "jpg") && ($extension != "jpeg") &&
                ($extension != "png") && ($extension != "gif")) 
        {
            //print error message
            $this->errorMsg = "You must choose a jpeg, jpg,
             png, or gif file";
            $this->errors=1;
        } else {
        //get the size of the image in bytes
        //$_FILES['image']['tmp_name'] is the temporary filename of the                              file
        //in which the uploaded file was stored on the server
        $size=filesize($_FILES['file']['tmp_name']);

        //Store the extension for listing in session
        $_SESSION["image_ext"] = $extension;

        //compare the size with the maxim size 
        //we defined and print error if bigger
        if ($size > MAX_SIZE*1024)
        {
            $this->errorMsg = "You have exceeded the size limit.";
            $this->errors=1;
        }


        //we will give an unique name, 
                   //for example the time in unix time format
        $image_name= $item_num.'.'.$extension;

        chdir("/home/public_html/");
        try {
        @mkdir($item_num, 0755);
        chmod($item_num, 0755);
        } catch (Exception $e) {

        } 

        //we verify if the image has been uploaded, and print error instead
        $imagePath = $_FILES['file']['tmp_name'];

        //the new name will be containing the full 
                   //path where will be stored (images folder)
        $newname= $item_num."/".$image_name;

        $copied = copy($imagePath, $newname);
        //Now use that directory andmake the image

        if (!$copied) 
        {
        echo "Failure";
        $this->errorMsg = "There was an error uploading your image.";
        $this->errors=1;
        }

    }
    //If no errors registred, print the success message
     if(isset($_POST['picture']) && !$this->errors) 
    {
                      echo "success";
            }
        }
于 2013-08-13T05:19:16.680 回答