[英]Contact form will not allow Null on file upload PHP
我希望我的联系表格允许文件上载Null。 当我尝试提交没有上载文件的表单时,它说:“对不起,文件已经存在。,对不起,只允许JPG,JPEG,PNG,GIF,TIF,EPS和PSD文件,对不起,您的文件尚未上传。” 当它为空或不安全时,我将如何更改代码以绕过它? 如果我没有添加足够的细节,请让我知道,我将为您提供详细信息。
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.