使用PHP服务器将文件上传到Web,无法正常工作

问题描述:

I am running a php server for test purposes on 127.0.0.1:8080, and I am trying to save an image to "web" with save2web.php:

  if (isset($_FILES)){
  //put parameters in variables
  $filename = $_FILES['file']['name'];
  $fileTempName = $_FILES['file']['tmp_name'];
  $fileType = $_FILES['file']['type'];
  $fileSize = $_FILES['file']['size'];
  $fileError = $_FILES['file']['error'];

//if the file is a JPG and under 400K, proceed:
if (($fileType == "image/jpeg") && ($fileSize < 400000)){
    //if there's a file error, print it:
    if ( $fileError > 0){
        echo "Return Code" . $fileError . "<br />";
    }
    //if there's no error, print some HTML about the file:
    else {
        echo "Upload: " . $fileName . "<br />";
        echo "Type: " . $fileType . "<br />";
        echo "Size: " . ($fileSize/1024) . " Kb<br />";
        echo "Temp file: " . $fileTempName . "<br />";

        //if the file already exists,
        //delete the previous version:
        if (file_exists($fileName)){
            unlink($fileName);
        }
        //move the file from the temp location to
        //this directory:
        move_uploaded_file($fileTempName, $fileName);
        echo "Uploaded file stored as: ".$filename;
        }
    }
    //if the file is not a jpg or too big, say so:
    else{
        echo "File is not a JPEG or too big.";
    }
}
 ?>

 <html>
 <body>
 <form action="save2web.php" method="post"
 enctype="multipart/form-data">
 <label for="file">Filename:</label>
 <input type="file" name="file" id="file" />
 <br />
 <input type="submit" name="submit" value="Upload" />
 </form>
 </body>
 </html>

when I run the script on the browser, however, I get:

 Upload: 
 Type: image/jpeg
 Size: 45.728515625 Kb
 Temp file: /private/var/tmp/phpEgyvKC

Warning: move_uploaded_file(): Filename cannot be empty in /Users/me/Documents/CODE/Processing/sketches_3/sketches/apps/NetworkedCat_ImageCaptureUpload/save2web.php on line 31

Warning: move_uploaded_file(): Unable to move '/private/var/tmp/phpEgyvKC' to '' in /Users/me/Documents/CODE/Processing/sketches_3/sketches/apps/NetworkedCat_ImageCaptureUpload/save2web.php on line 31 Uploaded file stored as: Peti2.jpg

"line 31" is move_uploaded_file()

this doesn't seem to be a permission issue, because I have changed permissions to chmod 777 on /private/var/tmp, as well as on destination folder

what am I missing?

set your path where you want to upload file

$path = 'your folder path';

move_uploaded_file($fileTempName, $path.$fileName);

destination it is not the directory, it's the file;

before all the code, set:

$path='img.jpg'

then:

move_uploaded_file($fileTempName, $path);

image will be uploaded there; and if the name of the image on pathis the same, file will be substituted.