PHP:当文件为';t通过联系方式上传至电子邮件

PHP:当文件为';t通过联系方式上传至电子邮件,php,email,contact-form,Php,Email,Contact Form,我正试图让表单跳过文件有效期和支票的代码 我被告知要使用:iF($_FILES[“fileToUpload”]==UPLOAD\u ERR\u NO\u FILE){ 我曾尝试在我的代码中实现这一点,但我是php新手,我想我可能弄乱了标题。现在,当我发送电子邮件时,无论是否没有附件,它都会跳过所有文件检查 如果我试图跳过代码,我已经注释了行 如果我没有提供足够的详细信息,请让我知道 <?php // Turn off all error reporting error_reporting(

我正试图让表单跳过文件有效期和支票的代码 我被告知要使用:
iF($_FILES[“fileToUpload”]==UPLOAD\u ERR\u NO\u 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
}

我需要用什么条件替换此项并将所有代码置于两者之间?是,将所有附件/文件相关代码置于此条件之间。当前您正在使用此条件,如果($\u FILES[“fileToUpload”]==上载\u ERR\u NO\u文件)…您可以用建议的条件替换此项。我已更新代码,但我收到的错误是文件已存在(抱歉,文件已存在。)(抱歉,仅允许使用JPG、JPEG、PNG、GIF、TIF、EPS和PSD文件。)(抱歉,您的文件未上载。)。当不上载任何内容时,当我上载文件时,我只会得到一个没有名称的附件,它是一个包含代码的文档。html表单中的文件类型字段名称是什么?type=“file”和name=“fileToUpload”
if(isset($_FILES["fileToUpload"]) && !empty($_FILES["fileToUpload"]["tmp_path"])){
 // your code here
}