我正在尝试让表单跳过文件代码和检查我被告知使用:iF($_FILES["fileToUpload"] == UPLOAD_ERR_NO_FILE) {
我试图在我的代码中实现这一点,但我是 php 的新手,我想我可能搞砸了标题。现在,当我发送不带附件的电子邮件时,它会跳过所有文件检查。
我已经评论了我尝试跳过代码的行。
如果我没有提供足够的信息详细信息,请告诉我。
<?php
// Turn off all error reporting
error_reporting(0);
// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);
// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
// Report all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);
// Report all PHP errors (see changelog)
error_reporting(E_ALL);
// Report all PHP errors
error_reporting(-1);
// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
$to = 'test@gmail.com';
$subject = 'Website Submission';
$company_name = $_POST['company_name'];
$ref = $_POST['ref'];
$website = $_POST['website'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$fromweb = $_POST['fromweb'];
$qr = $_POST['qr'];
$message = $_POST['message'];
$body = <<<EMAIL
<html>
<p><h3>Email from website.</h3></p>
<p><strong>Company Name:</strong> $company_name</p>
<p><strong>Ref:</strong> $ref</p>
<p><strong>Website:</strong> $website</p>
<p><strong>Email:</strong> $email</p>
<p><strong>Tel:</strong> $tel</p>
<p><strong>Create From Website:</strong> $fromweb</p>
<p><strong>Add QR Code:</strong> $qr</p>
<p><strong>File Location:</strong> $target_file</p>
<p><strong>Message:</strong> $message</p>
</html>
EMAIL;
///////////////////////////////////////////////////// '/
// SKIPS IF NO FILE
iF($_FILES["fileToUpload"] == UPLOAD_ERR_NO_FILE) {
// no file selected, do skip
$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>';
if($imageFileType !=null) {
echo "no file uploaded";
}
$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>';
}
}
/* Attachment File
Attachment location */
$file_name = $target_file;
$path = $file_name;
// Read the file content
$file = $file_name;
$file_size = filesize($file_name);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
/* Set the email header
Generate a boundary */
$boundary = md5(uniqid(time()));
// Email header
// $header = "From: ".$from_name." 'r'n";
} //skiping file upload
///////////////////////////////////////////////////// /'
$header = 'From: <noreply@email.co.uk>' . "'r'n";
// $header .= "Reply-To: ".$reply_to."'r'n";
$header .= "MIME-Version: 1.0'r'n";
// Multipart wraps the Email Content and Attachment
///////////////////////////////////////////////////// '/
// SKIPS IF NO FILE
iF($_FILES["fileToUpload"] == UPLOAD_ERR_NO_FILE) {
$header .= " boundary='"".$boundary."'"";
}
///////////////////////////////////////////////////// /'
//$message .= "This is a multi-part message in MIME format.'r'n'r'n";
/* Email content
Content-type can be text/plain or text/html */
// $message .= "Content-Type: text/plain; charset='"iso-8859-1'"'r'n";
// this header below is the important one if you want HTML message
$header .= 'Content-type: text/html; charset=iso-8859-1' . "'r'n";
$header .= "Content-Type: multipart/mixed;'r'n";
$message .= "'r'n";
$message .= "$body'r'n";
///////////////////////////////////////////////////////// '/
// SKIPS IF NO FILE
iF($_FILES["fileToUpload"] == UPLOAD_ERR_NO_FILE) {
$message .= " name='"".$file_name."'"'r'n";
$message .= " filename='"".$file_name."'"'r'n";
$message .= "'r'n".$content."'r'n";
/* Attachment
Edit content type for different file extensions */
$message .= "Content-Type: application/xml;'r'n";
$message .= "Content-Transfer-Encoding: base64'r'n";
$message .= "Content-Disposition: attachment;'r'n";
$message .= "--".$boundary."--'r'n";
}
///////////////////////////////////////////////////////// /'
if ($_POST['submit']){
mail($to, $subject, $message, $header);
echo '<p style="color:green;">Message Successfully Sent.</p>';
} else {
die('<p>Error Email Not Sent</p>');
}
?>
您已经在需要时添加了条件。
只需将条件替换为
if(isset($_FILES["fileToUpload"]) && !empty($_FILES["fileToUpload"]["tmp_path"])){
// your code here
}
您可以使用 is_uploaded_file():
if(!file_exists($_FILES['myfile']['tmp_name']) || !is_uploaded_file($_FILES['myfile']['tmp_name'])) {
echo 'No upload';
}