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

我一直在寻找通过上传文件 . 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 . 只有在我尝试移动临时文件时,此时才会出现任何错误迹象 .

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值