选择是否将文件保存为 blob ajax php
Option to save file or not in database as blob ajax php
我从 ajax 请求到 php 文件成功地将文件作为 blob 保存在数据库中。
我的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) {}
我的问题很简单,当我不想把文件放在 ok.Currently 当我不把任何东西放在 input(type file)
时,我怎么能做到这一点我总是得到错误在 $fileName = $_FILES['file']['name'];
中说 file
是未定义的,所以我不能将 if 条件设置为 if($fileSize = $_FILES['file']['size'] > 0)
or/and 我不能将 input(type file)
留空,因为我肯定会得到 undefined index for file
。我怎样才能让 input(type file)
为空并且不会出现错误 undefined index : file
。任何想法表示赞赏。
GOAL
在数据库中保存或不保存文件的灵活性
你必须检查 $_FILES['file']
是否存在。
if (isset($_FILES['file'])) {
// process file
}
根据 Keo 的回答,我试了一下,想出了这个
if (!empty($_FILES)) {
现在我可以根据我的要求保存和不保存。我实现了上传和不上传的目标,具体取决于我需要什么
我从 ajax 请求到 php 文件成功地将文件作为 blob 保存在数据库中。 我的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) {}
我的问题很简单,当我不想把文件放在 ok.Currently 当我不把任何东西放在 input(type file)
时,我怎么能做到这一点我总是得到错误在 $fileName = $_FILES['file']['name'];
中说 file
是未定义的,所以我不能将 if 条件设置为 if($fileSize = $_FILES['file']['size'] > 0)
or/and 我不能将 input(type file)
留空,因为我肯定会得到 undefined index for file
。我怎样才能让 input(type file)
为空并且不会出现错误 undefined index : file
。任何想法表示赞赏。
GOAL
在数据库中保存或不保存文件的灵活性
你必须检查 $_FILES['file']
是否存在。
if (isset($_FILES['file'])) {
// process file
}
根据 Keo 的回答,我试了一下,想出了这个
if (!empty($_FILES)) {
现在我可以根据我的要求保存和不保存。我实现了上传和不上传的目标,具体取决于我需要什么