简体   繁体   中英

Php move_uploaded_file, cant found the error

Please kindly point out my mistakes. Thanks a lot.

$photoName = $_POST['photoName']; // image name

$photoFile = $_POST['photoFile']; // image data

$uploadDir = 'upload/photo/'; // path to be uploaded

        if (!file_exists($uploadDir)) {
            mkdir($uploadDir, 0777, true);
        }

        $fileName = $_FILES['file']['$photoName'];
        $fileData = $_FILES['file']['$photoFile'];


        $filePath = $uploadDir.$fileName ;
        $moveFile = move_uploaded_file($fileData , $filePath);

The $ Inside '' is not parsed.

Use "" and it will work

$fileName = $_FILES['file']["$photoName"];
$fileData = $_FILES['file']["$photoFile"];

Or just without:

$fileName = $_FILES['file'][$photoName];
$fileData = $_FILES['file'][$photoFile];

You should have some error detection when dealing with files, if anything for making sure the file was moved properly, or exit gracefully for the end user. Please see the following code:

<?php
if(isset($_FILES['photoFile'])){
$photoFile = $_POST['photoFile']; // image data

$uploadDir = 'upload/photo/'; // path to be uploaded
$uploadfile = $uploaddir . basename($_FILES['photoFile']['name']);

    if (!file_exists($uploadDir)) {
        mkdir($uploadDir, 0777, true);
    }


echo "<p>";

// Error Checking Extended, view the php manual for other error codes to chain here.
    if($_FILES['photoFile']['error'] == 2) {
    echo "You've exceeded the maximum file upload size of 512kb.";
    return false;
    }

if (move_uploaded_file($_FILES['photoFile']['tmp_name'], $uploadfile)) {
  echo "File is valid, and was successfully uploaded.\n";
} else {
   echo "Upload failed";
}

echo "</p>";
} else {
?>
<form enctype="multipart/form-data" action="" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="512000" />
    Send this file: <input name="photoFile" type="file" />
    <input type="submit" value="Send File" />
</form>
<?php
}
?>

EDIT: Outputs:

File is valid, and was successfully uploaded.

Here is some more debugging info:Array
(
    [userfile] => Array
        (
            [name] => schema.sql
            [type] => application/octet-stream
            [tmp_name] => /tmp/phpNFBKRW
            [error] => 0
            [size] => 28729
        )

)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM