PHP:`move_uploaded_file`用于正确访问用户提交文件的`tmp_name`

时间:2015-02-14 02:17:13

标签: php forms file upload

过去几天我一直在努力通过表单提交图片并让我的php脚本将其保存在指定的目录中。我已尝试使用$.ajax,我已尝试使用XMLHttpRequest,现在我只是从here复制了一个更简单的脚本并进行了调整。

move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)评估为false并且我不相信会发出任何警告,因此根据documentation,访问该文件时出现问题。

脚本响应是

/private/var/tmp/phpwwBaBr
Sorry, there was an error uploading your file.

此外,echo $_FILE['fileToUpload']['error'];的错误代码为0,这意味着上传成功,但../includes/galleryImages/中仍无文件。


以下是代码:

<form action="archive_upload.php" id='file_form' method="post" enctype="multipart/form-data" accept-charset='UTF-8'>
     Select image to upload:
     <input type="file" name="fileToUpload" id="fileToUpload">
     <input type="submit" value="Upload Image" name="submit">
</form>

<?php
    $target_dir = "../includes/galleryImages/";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    $uploadOk = 1;
    if(isset($_POST["submit"])) {
        $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
        if($check !== false) {
            $uploadOk = 1;
        } else {
            $uploadOk = 0;
        }
    }

    echo $_FILES["fileToUpload"]["tmp_name"].'<br>';    

    /* This is the line that returns false */
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo basename( $_FILES["fileToUpload"]["name"] ). " has been uploaded.<br>";

    } else {
        echo "Error uploading file.<br>";
    }
?>

0 个答案:

没有答案