2011-10-14 57 views
0

我有一段代碼正在處理用戶上傳的文件並對其進行處理。當用戶上傳.xls文件時,該文件被粉碎。我懷疑它與MIME有關,但我不太瞭解它們。任何幫助,將不勝感激。通過PHP粉碎上傳文件.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">'; 
} 

} 
?> 
+2

也許是我,你是什麼意思撕碎? – Duniyadnd

+1

損壞,無法正常工作等 –

+1

這可能不是[可重現的](http://sscce.org/) - 「$ files」的關鍵定義缺失。另外,我看不到上傳或下載。您似乎正在準備一封電子郵件,而不是接收或發送文件上傳。內容類型當然是錯誤的,它不應該在引號或括號中。另外,很可能,在處理上傳到您的網頁時,您不需要自己執行任何MIME處理。相反,只需訪問'$ _FILES'。 – phihag

回答

0

在什麼階段文件被損壞?在文件上傳後立即在服務器端,還是在文件通過電子郵件發送到電子郵件客戶端?

這裏是代碼重寫,使其可讀/標準兼容...

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

謝謝。該文件似乎立即被損壞。 –

+0

你的意思是,一旦它上傳後到達服務器?如果是這樣,你有嘗試過不同的瀏覽器嗎? – DaveRandom

+0

對不起,誤解了你的問題。該文件在用戶收到的電子郵件中被損壞。它聲明文件類型擴展名已被修改(儘管它仍然是.xls),然後顯示亂碼。我幾乎肯定它與MIME類型有關,但不知道如何進行診斷。 –