gpt4 book ai didi

php - 通过文件上传缺少 PHP 中的临时文件。 Ajax

转载 作者:行者123 更新时间:2023-11-28 08:56:05 27 4
gpt4 key购买 nike

我一直在考虑通过上传文件。使用 FormData 和 FileReader 进行 AJAX,但我一直遇到同样的问题。

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

Warning: move_uploaded_file([path\to\temp\directory]\php256.tmp): failed to open stream: No such file or directory in [path/to/script/file.php] on line [line]

Warning: move_uploaded_file(): Unable to move '[path\to\temp\directory]\php256.tmp' to '[path\to\permanent\directory]\[filename.ext]' in [path/to/script/file.php] on line [line]

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

我的 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。仅当我尝试移动临时文件时,此时才没有任何错误迹象。

如果您能就这个问题提供任何帮助,我们将不胜感激!

最佳答案

Geht ned mit js ... 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.

关于php - 通过文件上传缺少 PHP 中的临时文件。 Ajax ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18470652/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com