选择保存文件或不在数据库中作为blob ajax php

时间:2015-03-05 10:21:35

标签: php mysql ajax file blob

我成功地将数据库中的文件保存为从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) {}

我的问题很简单,我怎么能以这样的方式制作它,当我不想把文件放好时。目前当我不在input(type file)中放任何东西时我总是在{{{ 1}}说$fileName = $_FILES['file']['name'];未定义,所以我不能将if条件设为file或/并且我不能让if($fileSize = $_FILES['file']['size'] > 0)为空,因为我肯定会得到input(type file)。如何才能将undefined index for file留空并且不会收到错误input(type file)。任何想法都表示赞赏。

undefined index : file

保存或不保存数据库中文件的灵活性

2 个答案:

答案 0 :(得分:3)

您必须检查$_FILES['file']是否存在。

if (isset($_FILES['file'])) {
    // process file
}

答案 1 :(得分:1)

基于Keo的回答我玩了它,我想出了这个

if (!empty($_FILES)) {

现在我可以保存而不是根据我的要求保存。我的目标是上传而不是根据我的需要上传