首页 文章

从文件上传通过PHP丢失PHP中的临时文件 . AJAX

提问于
浏览
1

我一直在寻找通过上传文件 . AJAX使用FormData和FileReader,但我一直遇到同样的问题 .

触发文件上载后,会将其传递给PHP脚本,该脚本验证上载的文件并尝试将其移动到永久目录中 . 不幸的是,虽然验证成功,但调用 move_uploaded_file() 会产生以下警告:

警告:move_uploaded_file([path \ to \ temp \ directory] \ php256.tmp):无法打开流:[line / to / script / file.php]中没有这样的文件或目录[line]警告:move_uploaded_file ():无法将[path \ to \ temp \ directory] \ php256.tmp'移动到[path / to / script / file.php中的'[path \ to \ permanent \ directory] \ [filename.ext]' ]在线[线]

通过正常提交表单上传文件并移动临时文件按预期工作 .

我的php.ini设置非常标准:

file_uploads = On
upload_tmp_dir = "[path\to\temp\directory]"
upload_max_filesize = 2M
max_file_uploads = 20

我的PHP也应该是非常标准的:

$fileField = 'target_file';

if(!(isset($_FILES) && is_array($_FILES) && isset($_FILES[$fileField]))) {
    // No File Sent (Error Out)
}

$file = $_FILES[$fileField];

// Errors in transfer?
if(isset($file['error'])) {

    $haltMessage = false;

    switch($file['error']) {
        /* Switchboard checking through all standard file upload errors. And, of course, ERR_OK */
    }

    if($haltMessage !== false) {

        // An error occured! Error Out

    }

}

// Check if the file was uploaded.
if(!is_uploaded_file($file['tmp_name'])) {

    // No File Uploaded (Error Out)

}

if(/* File type checking.. */)) {

    // Invalid file type (Error Out)

}

$uploadDir = 'path/to/permanent/dir';

$fileName = $file['name'];

$location = $uploadDir . DS . basename($fileName); // DS = '/'

$result = move_uploaded_file($file['tmp_name'], $location);

if($result) {

    // Yes!

} else {

    // Something did indeed go wrong.
    // This block of code is ran after the warnings are issued.


}

所以这让我的JavaScript有点需要:

(function($) {


    $(document).ready(function(){

        var url = 'url/to/php/script.php',
            input,
            formdata;

        // FormData support? (We have a fallback if not)
        if(window.FormData != null && window.FileReader != null) {

            input = document.getElementById('target_file');
            formdata = new FormData(),

            input.addEventListener('change', function(ev) {

                var reader,
                    file;

                if(this.files.length === 1) {

                    file = this.files[0];

                    if(!!file.type.match(/* File type matching */)) {

                        reader = new FileReader();
                        reader.onloadend = function (e) {

                            // Uploaded Handle

                        }

                        reader.onload = function() {

                            formdata.append('target_file', file);

                            $.ajax({
                                url : url,
                                type : "POST",
                                data : formdata,
                                processData: false,
                                contentType: false,
                                success : function(res) {
                                    console.log(res);
                                }
                            });

                        }

                        reader.readAsDataURL(file);

                    } else {
                        // Invalid file type.
                    }

                } else {
                    // No file / too many files (hack) selected.
                }

            }, false);

        }
    });
}(jQuery));

我正在使用FireFox 23.0.1,我在服务器上运行PHP 5.4.7 .

为长期问题道歉,但我不确定问题出在哪里 . 转储$ _FILES变量会显示一个充满预期文件信息的数组,包括tmp_name . 只有在我尝试移动临时文件时,此时才会出现任何错误迹象 .

如果您对此问题有任何帮助,我们将不胜感激!

1 回答

  • 0

    非常麻烦...... Link:

    move_uploaded_file() ist von den normalen Safe Mode UID-Einschränkungen nicht betroffen. Dies ist nicht unsicher, da move_uploaded_file() nur mit via **PHP hochgeladenen Dateien** arbeitet.
    

相关问题