Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过PHP Shreding.xls文件上传文件_Php_Excel_Mime Types_Mime - Fatal编程技术网

通过PHP Shreding.xls文件上传文件

通过PHP Shreding.xls文件上传文件,php,excel,mime-types,mime,Php,Excel,Mime Types,Mime,我有一段代码,它获取用户上传的文件,并对其进行处理。当用户上载.xls文件时,该文件将被分解。我怀疑这和哑剧有关,但我对他们了解不多。任何帮助都将不胜感激 <?php include ("header1.html") ?> <!--- End ---> <tr height="100%"> <td align="center" valign="top" style="background-image: url('images/midbg.jpg'

我有一段代码,它获取用户上传的文件,并对其进行处理。当用户上载.xls文件时,该文件将被分解。我怀疑这和哑剧有关,但我对他们了解不多。任何帮助都将不胜感激

<?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">';
}

}
?>

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

下面是重新编写的代码,以使其可读/符合标准

$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 .= '--';

也许是我,你说的碎纸是什么意思?这可能不是-$files的关键定义缺失了。此外,我看不到上传或下载。您似乎正在准备电子邮件,而不是接收或发送文件上载。内容类型当然是错误的,不应该是引号或括号。而且,在处理上传到您的网页时,您不需要自己进行任何MIME处理。相反,只需访问$\u文件。@Ed。哦,关于切碎:究竟发生了什么切碎?你能给我们一个切碎文件和原始文件的链接吗?@phihag-谢谢…我会用更多细节更新代码。对不起,是的,我正在准备一封电子邮件。这是字节比较的屏幕截图,原图在左边。出于安全考虑,我无法发送这些文件。这不太可能是问题所在,但请注意,规范规定您应该使用\r\n而不仅仅是\r\n来分隔标题等。大多数客户端都很聪明,可以进行补偿,但最好严格遵守标准,这样就不会有含糊不清的地方了……谢谢。文件似乎马上就被损坏了。你的意思是,一旦上传到服务器上,文件就被损坏了?如果是,您是否尝试过其他浏览器?对不起,误读了您的问题。该文件在用户收到的电子邮件中已损坏。它声明文件类型扩展名已通过still.xls进行了修改,然后显示jibberish。我几乎可以肯定这与MIME类型有关,但我不知道如何进行诊断。MIME类型不应导致此问题,尤其是在使用应用程序/八位字节流时。查看您发布的示例字节比较,看起来所有出现的值大于127 7F的字节都已被问号3F替换,尽管我必须检查整个比较以确认这一点。这向我表明,问题是由于二进制文件被解释为7位编码,可能字节被视为有符号?在这一行的某个地方,这是在消耗标准128字符ASCII表中不包含的字节。我想你是对的。我对这两个比较进行了更详细的研究,整个集合中都出现了这种模式。我是内容传输编码:7比特\r\n知道我是否应该使用其他编码吗?