选择是否将文件保存在数据库中作为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) {}

我的问题很简单,当我不想放文件时,我该如何做。目前,当我没有在input(type file)中放任何东西时,我总是在$fileName = $_FILES['file']['name'];中出错,说file未定义,所以我不能把if条件设为if($fileSize = $_FILES['file']['size'] > 0)或/,我不能让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)) {

现在我可以根据自己的要求保存和不保存。我实现了上传和不上传的目标,这取决于我需要什么