我成功地将文件从ajax请求保存为数据库中的blob到php文件。 我的ajax是这样的
var file = $('#fileuploader')[0].files[0];
var formData = new FormData();
formData.append('file', file);
}
$.ajax({
url: '../include/Addnewstudent.php',
type: 'POST',
dataType: "json",
data: formData,
processData: false, // tell jQuery not to process the data
contentType: false, // tell jQuery not to set contentType
success: function(data) {
console.log(data);
// window.location.reload(true);
}, error: function(data) {
alert("Error!"); // Optional
//window.location.reload(true);
}
});
在我的 PHP 中我有
$fileName = $_FILES['file']['name'];
$fileType = $_FILES['file']['type'];
$fileSize = $_FILES['file']['size'];
$fileError = $_FILES['file']['error'];
我在哪里检查文件
if ($fileError == UPLOAD_ERR_OK) {}
我的问题很简单,当我不想放入文件时,如何才能做到这一点。目前,当我不在输入(类型文件)
中放入任何内容时,我总是在 $fileName = $_FILES['file']['name'];
中出现错误,表示 file
未定义,因此我无法将 if 条件设置为 if($fileSize = $_FILES['file']['size'] > 0)
或/并且我不能将 input(type file)
留空,因为我肯定会得到文件未定义索引
。我怎样才能将input(type file)
留空并且不出现错误undefined index : file
。任何想法都会受到赞赏。
目标
可以灵活地在数据库中保存或不保存文件
最佳答案
您必须检查$_FILES['file']
是否存在。
if (isset($_FILES['file'])) {
// process file
}
关于php - 选择是否将文件保存在数据库中作为 blob ajax php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28875426/