【问题标题】:Uploading File via PHP shredding .xls files通过 PHP 粉碎 .xls 文件上传文件
【发布时间】:2011-12-07 18:48:52
【问题描述】:

我有一段代码正在获取用户上传的文件并对其进行处理。当用户上传 .xls 文件时,该文件被粉碎。我怀疑这与 MIME 有关,但我对它们了解不多。任何帮助将不胜感激。

<?php include ("header1.html") ?>
<!--- End --->
<tr height="100%">
    <td align="center" valign="top" style="background-image: url('images/midbg.jpg'); background-repeat:repeat-x; padding-top: 25px; " bgcolor="#e6e6e6" >

    <!--- Body begins here --->

    <table width="725"  border="0" cellspacing="0" cellpadding="2">
     <tr>
     <td width="100%" valign="top">
     <table style="margin-left:130px; margin-top:20px;">
<tr><td>
<p><strong style="font-size:12px"> </strong> </p>
<?php

$userName = $session->userName;

if ($handle = opendir('fileuploads/'.$userName)) {
    //echo "Directory handle: $handle\n";
   // echo "Files:\n";
    $path = 'fileuploads/'.$userName.'/';
    /* This is the correct way to loop over the directory. */
    while (false !== ($file = readdir($handle))) {
        if(($file != "Thumbs.db") &&  ($file != ".")&&  ($file != ".."))
    {
    $attachment[] = $path.$file;
    }
}
//  echo '<p><b>Current total = '.$totalsize.'K</b></p>';
closedir($handle);
} 


    function fileName($inputfile,$userName)
{
    $separator = '/'.$userName.'/';
$output = split ($separator, $inputfile);
return $output[1];
}

$files = $attachment;
//print_r($files);
// email fields: to, from, subject, and so on
 $memberEmails = $_POST['emails'];
  $bcc = $_POST['bccAddress'];

if ($bcc != '')
{
$bcc = $memberEmails . ',' . $bcc; 
}
else
{
 $bcc = $memberEmails;
}

$to = $_POST['toAddress'];
if($to != '')
{
$to = $userName. "@place.com,". $to;
}
else
{
$to = $userName. "@place.com";
}
$cc = $_POST['ccAddress'];
$from = $userName. "@place.com"; 
$subject =$_POST['subject']; 
$message = $_POST['content'];
$message = str_replace('\"', '"',$message);
$headers = "From: ".$_SESSION['fullName']. "<$from>\n";

// boundary 
$semi_rand = md5(time()); 
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
// headers for attachment 
if ($cc != '')
{
$headers .= "CC: ". $cc ."\n";
}

if ($bcc != '')
{
$headers .= "BCC: ". $bcc ."\n";
}
$headers .= "MIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"    {$mime_boundary}\""; 
// multipart boundary 
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 

if( count($files) > 0)
{
$message .= "--{$mime_boundary}\n";
}

// preparing attachments
for($x=0;$x<count($files);$x++){
$file = fopen($files[$x],"rb");
$data = fread($file,filesize($files[$x]));
fclose($file);
$fileName= fileName($files[$x],$userName);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . 
"Content-Disposition: attachment;\n" . " filename=\"$fileName\"\n" . 
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$y = $x +1;
if ( count($files) > $y)
{
     $message .= "--{$mime_boundary}\n";

}
}




$ok = @mail($to, $subject, $message, $headers); 


   if ($ok)
{
$logFile = "log/tmlog.log";
$logHandle = fopen($logFile, 'a');
$logData = "[" . date('Y-m-d H:i:s') . "] " .$userName. " - message sent successfully (". $to. ",".$bcc .",". $cc.")\n";
fwrite($logHandle, $logData);
fclose($logHandle);
echo '<META HTTP-EQUIV="Refresh" CONTENT="0;URL=fileuploads/sendRm.php?msg=sent">';
}
else
{
$logFile = "log/tmlog.log";
$logHandle = fopen($logFile, 'a');
$logData = "[" . date('Y-m-d H:i:s') . "] " .$userName. " - message failed\n";
fwrite($logHandle, $logData);
fclose($logHandle);
echo '<META HTTP-EQUIV="Refresh" CONTENT="0;URL=fileuploads/sendRm.php?msg=fail">';
}

}
?>

【问题讨论】:

  • 可能是我,你说的撕碎是什么意思?
  • 这可能不是reproducible - 缺少$files 的关键定义。另外,我看不到上传或下载。您似乎正在准备一封电子邮件,而不是接收或发送文件上传。 Content-Type 当然是错误的,它不应该是引号或括号。此外,在处理上传到您的网页时,您很可能不需要自己进行任何 MIME 处理。相反,只需访问$_FILES
  • @Ed。哦,关于切碎:究竟发生了什么切碎?你能给我们一个切碎的文件和原始文件的链接吗?
  • @phihag - 谢谢...我会用更多细节更新代码。对不起,是的,我正在准备一封电子邮件。这是字节比较的屏幕截图,左边是原始的。出于安全考虑,我无法发送文件。 i55.tinypic.com/244wako.png
  • 不太可能是问题,但请注意规范说您应该使用\r\n 而不是仅仅使用\n 来分隔标题等。大多数客户都足够聪明,可以补偿,但最好是严格遵守标准,以免产生歧义......

标签: php excel mime-types mime


【解决方案1】:

文件在哪个阶段损坏?文件上传后立即在服务器端,还是文件通过电子邮件发送后在电子邮件客户端?

这是为了使其可读/符合标准而重写的代码...

$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

// headers for attachment 
if ($cc != '') $headers .= "Cc: $cc\r\n";    
if ($bcc != '') $headers .= "Bcc: $bcc\r\n";
$headers .= "MIME-Version: 1.0\r\n"
          . "Content-Type: multipart/mixed; boundary=\"$mime_boundary\"\r\n";

// multipart boundary 
$message = "This is a multi-part message in MIME format.\r\n"
         . "--$mime_boundary\r\n"
         . "Content-Type: text/html; charset=\"iso-8859-1\"\r\n"
         . "Content-Transfer-Encoding: 7bit\r\n"
         . "\r\n"
         . $message . "\r\n"
         . "--$mime_boundary";

if (count($files)) { // Add attachments
  for ($x = 0; $x < count($files); $x++){
    $data = chunk_split(base64_encode(file_get_contents($files[$x])));
    $fileName = fileName($files[$x], $userName);
    $message .= "\r\n"
              . "Content-Type: application/octet-stream\r\n"
              . "Content-Disposition: attachment; filename=\"$fileName\"\r\n"
              . "Content-Transfer-Encoding: base64\r\n"
              . "\r\n"
              . $data . "\r\n"
              . "--$mime_boundary";
  }
}

$message .= '--';

【讨论】:

  • 谢谢。该文件似乎立即损坏了。
  • 你的意思是上传后一到服务器?如果是这样,您是否尝试过其他浏览器?
  • 抱歉,误读了您的问题。用户收到的电子邮件中的文件已损坏。它指出文件类型扩展名已被修改(尽管它仍然是 .xls),然后显示乱码。我几乎肯定它与 MIME 类型有关,但不知道如何诊断。
  • MIME 类型不应导致此问题,尤其是当您使用 application/octet-stream 时。查看您发布的示例字节比较,看起来所有出现的值大于 127 (7F) 的字节都已被问号 (3F) 替换,尽管我必须检查整个比较来确认这一点。这向我表明问题是由于二进制文件被解释为好像它是 7 位编码(可能字节被视为有符号?)沿线某处,这是吃不属于标准的字节128 个字符的 ASCII 表。
  • 我认为你是对的。我对这两个比较进行了更详细的研究,并且整个系列都出现了这种模式。我是 . "Content-Transfer-Encoding: 7bit\r\n" 知道是否应该使用其他东西吗?
猜你喜欢
  • 2010-11-06
  • 1970-01-01
  • 2020-01-24
  • 1970-01-01
  • 2017-06-25
  • 2014-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多