警告:file()[function.file]:文件名不能为空。为什么?

时间:2014-11-18 05:55:32

标签: php email

当我要提交时(警告:file()[function.file]:文件名不能为空)警告显示。我怎么解决这个问题。请帮助我是php的新手。电子邮件已发送但文件未附加。我想发送附带原始php的邮件。

可能代码如下:

    $tmpName = $_FILES['attachment']['tmp_name'];
                    $fileType = $_FILES['attachment']['type'];
                    $fileName = $_FILES['attachment']['name'];

                    /* Start of headers */
                   $headers = "From: $fromName";

                  if (file($tmpName)) {
                  /* Reading file ('rb' = read binary)  */
                 $file = fopen($tmpName,'rb');
                 $data = fread($file,filesize($tmpName));
                 fclose($file);

                 /* a boundary string */
                $randomVal = md5(time());
                $mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";

                /* Header for File Attachment */
                  $headers .= "\nMIME-Version: 1.0\n";
                 $headers .= "Content-Type: multipart/mixed;\n" ;
                 $headers .= " boundary=\"{$mimeBoundary}\"";

             /* Multipart Boundary above message */
             $message = "This is a multi-part message in MIME format.\n\n" .
             "--{$mimeBoundary}\n" .
            "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
              "Content-Transfer-Encoding: 7bit\n\n" .
               $message . "\n\n";

             /* Encoding file data */
              $data = chunk_split(base64_encode($data));

             /* Adding attchment-file to message*/
             $message .= "--{$mimeBoundary}\n" .
             "Content-Type: {$fileType};\n" .
             " name=\"{$fileName}\"\n" .
             "Content-Transfer-Encoding: base64\n\n" .
             $data . "\n\n" .
  "--{$mimeBoundary}--\n";
}

 echo $message;
$sendmail = mail ("$to", "$subject", "$message", "$headers");

if($sendmail){
  echo "A email has been sent to: $to";
 }
else{
  echo "Error in Email sending";
}

}

1 个答案:

答案 0 :(得分:0)

if (file($tmpName)) {

你可能想要if (file_exists($tmpName)) {

同时确保$tmpName实际填写完毕。