在更改输入类型文件时,我尝试通过ajax上传文件(而不是通过表单提交),将文件移动到我选择的临时目录,然后传回给那个临时文件的路径文件然后处理。我使用CentOS Web Panel作为平台,我的ajax代码正常工作
$(document).on("change", '.dropify', function() {
var myFormData = new FormData();
var fil = this.files[0];
myFormData.append('file', fil);
console.log(myFormData);
$.ajax({
url: 'classes/images/upload.php',
type: 'POST',
dataType: 'text',
contentType: false,
processData: false,
data: myFormData,
success: function(response) {
console.log(response);
}
});
});
当这个文件到达PHP时,它会进入sys_temp_dir
,我无法通过move_uploaded_file()
移动文件,我怎样才能使用upload_tmp_dir
?或强制它完全上传到另一个目录?我宁愿不使用sys temp dir,因为我有会话等生活在其中。下面的PHP供参考。
if (isset($_FILES['file']['name'])) {
if (0 < $_FILES['file']['error']) {
echo 'Error during file upload' . $_FILES['file']['error'];
} else {
if (file_exists('images/uploads/' . $_FILES['file']['name'])) {
echo 'File already exists : images/uploads/' . $_FILES['file']['name'];
} else {
move_uploaded_file($_FILES['file']['tmp_name'], 'images/uploads/' . $_FILES['file']['name']);
echo 'File successfully uploaded : images/uploads/' . $_FILES['file']['name'];
}
}
} else {
echo 'Please choose a file';
}
下面的错误日志示例
[Mon Jul 17 13:01:25.742990 2017] [:error] [pid 212450:tid 139781212186368] [client 192.168.10.88:56517] PHP警告:move_uploaded_file(images / uploads / logo.jpg):打开失败stream:第13行的/home//public_html/classes/images/upload.php中没有这样的文件或目录,参考:http:// / fragrances-new。 PHP?
[Mon Jul 17 13:01:25.743071 2017] [:error] [pid 212450:tid 139781212186368] [client 192.168.10.88:56517] PHP警告:move_uploaded_file():无法移动&#39; / tmp / phpcvmedN&#39;到&#39; images / uploads / logo.jpg&#39;在第13行的/home//public_html/classes/images/upload.php,引用:http:// /fragrances-new.php?
答案 0 :(得分:1)
您可以尝试将文件从一个文件夹移动到另一个文件夹
if(!is_dir($destination_folder)) {
mkdir($destination_folder, 0777,true);
}
if(copy($source_folder, $destination_folder)) {
unlink($source_folder);
}
对我有用..
答案 1 :(得分:0)
你只需像这样设置你的js文件,然后你就会获得所有表单数据
$(document).on("change", '.dropify', function() {
var myFormData = new FormData(document.getElementById("form-id"));
$.ajax({
url: 'classes/images/upload.php',
type: 'POST',
dataType: 'text',
contentType: false,
processData: false,
data: myFormData,
success: function(response) {
console.log(response);
}
});
});