我希望我的联系表单允许在文件上传时使用Null。当我尝试提交没有上传文件的表格时,它说:“抱歉,文件已存在。很抱歉,只允许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>';
}
}
答案 0 :(得分:1)
您可以查看:
iF($_FILES["fileToUpload"] == UPLOAD_ERR_NO_FILE) {
// no file selected, do skip
}