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


Warning: file() [function.file]: Filename cannot be empty. Why?

当我要提交这个(警告: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";
}
}

if (file($tmpName)) {

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

还要确保$tmpName实际填写。