1

我有以下表格:

 <form method="POST" name="email" action="sendemail.php" enctype="multipart/form-data">
    <table border=0 cellPadding=5 cellSpacing=10 width=100%>
        <tr>
            <td><label for='position'>Position:</label></td>
            <td><input type=text name=position class=position value="" READONLY size=35 /></td>
        </tr>
        <tr>
            <td><label for='uploaded_file'>Resume:</label></td>
            <td><input type="file" name="uploaded_file"></td>
        </tr>
        <tr>
            <td colSpan=2 align=center><input class=subbtn type="submit" value="Submit" name='submit' /></td>
        </tr>
        <tr>
            <td colSpan=2><hr /></td>
        </tr>
        <tr>
            <td colSpan=2 align=center style="padding-top: 5px;"><input type=button value="Close Window" class="close" /></td>
        </tr>
    </table>
</form>

和以下 sendemail.php:

<?php

$uf = $_POST['uploaded_file'];

  $to = "myemail@yahoo.com";
  $subject = "This is subject";
  $message = "This is test message.";
  # Open a file
  $file = fopen( $uf, "r" );
 if( $file == false )
  {
     echo "Error in opening file";
     exit();
  }
  # Read the file into a variable
  $size = filesize($file);
  $content = fread( $file, $size);

  # encode the data for safe transit
  # and insert \r\n after every 76 chars.
  $encoded_content = chunk_split( base64_encode($content));

  # Get a random 32 bit number using time() as seed.
  $num = md5( time() );

  # Define the main headers.
  $header = "From:myemail@yahoo.com\r\n";
  $header .= "MIME-Version: 1.0\r\n";
  $header .= "Content-Type: multipart/mixed; ";
  $header .= "boundary=$num\r\n";
  $header .= "--$num\r\n";

  # Define the message section
  $header .= "Content-Type: text/plain\r\n";
  $header .= "Content-Transfer-Encoding:8bit\r\n\n";
  $header .= "$message\r\n";
  $header .= "--$num\r\n";

  # Define the attachment section
  $header .= "Content-Type:  multipart/mixed; ";
  $header .= "name=\"test.txt\"\r\n";
  $header .= "Content-Transfer-Encoding:base64\r\n";
  $header .= "Content-Disposition:attachment; ";
  $header .= "filename=\"test.txt\"\r\n\n";
  $header .= "$encoded_content\r\n";
  $header .= "--$num--";

  # Send email now
  $retval = mail ( $to, $subject, "", $header );
  if( $retval == true )
   {
      echo "Message sent successfully...";
   }
   else
   {
      echo "Message could not be sent...";
   }
?>

当我尝试使用表单发送简历时,出现以下错误:

打开文件时出错

有人可以告诉我问题出在哪里以及如何解决。

4

3 回答 3

2

上传的文件不在 $_POST 数组中,它们在 $_FILES 数组中。

所以使用 $_FILES['uploaded_file']['tmp_name'] 而不是 $_POST['uploaded_file']

有关 $_FILES 的进一步实现,请参见http://www.php.net/manual/en/features.file-upload.post-method.php

上传文件时还请注意安全问题,并使用 PHP 中的 is_uploaded_file() 函数验证文件。

于 2013-04-26T14:09:03.500 回答
1
<?php

$to = $_POST['toEmail'];
$fromEmail = $_POST['fieldFormEmail'];
$fromName = $_POST['fieldFormName'];
$subject = $_POST['fieldSubject'];

/* GET File Variables */
$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";
}

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

if($flgchk){
  echo "<script language='javascript' type='text/javascript'>
        alert('Success');
        window.location = 'index.php';
    </script>";
 }
else{
  echo "<script language='javascript' type='text/javascript'>
        alert('Message failed');
        window.location = 'index.php';
    </script>";
}
?>
于 2014-08-13T12:11:50.917 回答
-1

使用 === 而不是 ==

if( $file === false )

或者

 if( !$file )
于 2013-04-26T14:08:05.490 回答