联系表单将不允许在文件上传 PHP 时为空


Contact form will not allow Null on file upload PHP

我希望我的联系表格允许文件上传时为空。 当我尝试在没有上传文件的情况下提交表单时,它说"对不起,文件已经存在,对不起,只允许 JPG、JPEG、PNG、GIF、TIF、EPS 和 PSD 文件,抱歉,您的文件未上传。当代码是 emtpy 或不安全时,我将如何更改代码以绕过它?如果我没有添加足够的细节,请告诉我,我会先行介绍。

.PHP

  $target_dir = "uploads/";
            $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
            $uploadOk = 1;
            $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
            // Check if image file is a actual image or fake image
            // Check if file already exists
            if (file_exists($target_file)) {
                echo '<p style="color:red;">Sorry, file already exists.</p>';
                $uploadOk = 0;
            }
            // Check file size
            if ($_FILES["fileToUpload"]["size"] > 150000000) { // Byte = 150MB
                echo '<p style="color:red;">Sorry, your file is larger than 150MB.</p>';
                $uploadOk = 0;
            }
            // Allow certain file formats
            if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
            && $imageFileType != "gif" && $imageFileType != "eps" && $imageFileType != "tiff" 
            && $imageFileType != "psd") {
            echo '<p style="color:red;">Sorry, only JPG, JPEG, PNG, GIF, TIF, EPS and PSD files are allowed.</p>';
                $uploadOk = 0;
            }
            // Check if $uploadOk is set to 0 by an error
            if ($uploadOk == 0) {
                die('<p style="color:red;">Sorry, your file was not uploaded.</p>');
            // if everything is ok, try to upload file
            } else {
                if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
                    echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
                } else {
                    echo '<p style="color:red;">Sorry, there was an error uploading your file.</p>';
                }
            }

你可以检查一下:

iF($_FILES["fileToUpload"] == UPLOAD_ERR_NO_FILE) {
    // no file selected, do skip
}