2012-09-06 113 views
0

在追加附件時,我收到一條警告消息,該消息阻止我將現有的pdf文件附加到我的電子郵件中。我知道這不是我的頭文件,因爲腳本已經成功地附加了一個由它之前的字符串生成的vcard。 因爲我沒有編輯文件,所以不需要TDPDF & FPDF。 (雖然不是100%)這是我一直在使用的代碼。我已經包含了我的輸出測試行&評論。防止file_get_content正確讀取的警告

//File Definition 
     $filepath = "docs/"; 
     //$filepath = "./docs/"; //tried: same result 
     $fullpath = $filepath . $filename; //$filename defined earlier 

    //File Manipulation 
     $file = fopen($fullpath,'rb'); 
     $pdfdata = fread($file, filesize($fullpath)); 
     fclose($file); 

    //Testing 
     echo $fullpath . "<br />\n"; 
     echo "Filename: " .$filename . "<br />\n"; 
     echo "Filesize: " .filesize($fullpath). "<br />\n"; 
     echo "String Length: " .strlen($pdfdata). "<br />\n"; 

    //The Following line proved the variable is dumping properly, 
    //but its content cannot be used for file_get_contents...huh? 
     //var_dump($pdfdata); //Only used for proofing 

     echo "Probable Errors for file_get_contents<br />\n"; 
     $data = file_get_contents($pdfdata); 

    // The following line: Sends File, but is 0 bytes 
     //$attachment = chunk_split(base64_encode($pdfdata)); 
    //default 
     $attachment = chunk_split(base64_encode(file_get_contents($pdfdata))); 

此輸出:$ pdfdata & $文件大小:

docs/pdf-to-send.pdf 
Filename: pdf-to-send.pdf 
Filesize: 37907 
String Length: 37907 
Probable Errors for file_get_contents 

Warning: file_get_contents(%PDF-1.5 % ... (truncated by me) 
     ...) [function.file-get-contents]: failed to open stream: No such file or directory in /my/hosting/directory/mailer.php on line 337 
Warning: file_get_contents(%PDF-1.5 % ... (truncated by me) 
     ...) [function.file-get-contents]: failed to open stream: No such file or directory in /my/hosting/directory/mailer.php on line 339 

它告訴我的文件大小,可以在2分不同的變量被發現。他們匹配。我會提到我截斷的響應(由於字符集)已被服務器截斷。爲什麼我開始檢查長度。

最後,以防萬一它可能是我的頭,因爲我是能夠成功地發送一個0字節的文件,這裏是指那些線條...

$body .= "--". $mime_boundary. "\r\n" ; 
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"". "\r\n"; 
$body .= "Content-Transfer-Encoding: base64" . "\r\n"; 
$body .= "Content-Disposition: attachment;" . "\r\n"; 
$body .= "filename=\"".$filename."\"" . "\r\n\n"; 
$body .= $attachment . "\n\n"; 

我知道我可以改變(和已經嘗試過)「Content-Type」爲「application/pdf」。

我的字符集是UTF-8。我可能會誤解fopen()& fread()的「二進制安全」描述,但這不應該導致腳本失敗。應該是?

任何幫助解決這將不勝感激。

+0

'$ pdfdata'是PDF文件的**內容**。你爲什麼要用'file_get_contents'呢? – Passerby

+0

要添加到Passerby,對'file_get_contents'的調用是多餘的 - 當您調用'fread'時,您已經將文檔文本讀入變量'$ pdfdata'中。 – Kevin

+0

@Passerby謝謝。這個調用根本就沒有必要,我正在使用我發現正在使用'file_get_contents'的例子。這就是爲什麼我測試'strlen($ pdfdata)',因爲它沒有意義。希望我能+1您的評論。 – Kamikaze478

回答

1

好的。我修好了它。奇怪的是,它在我的頭文件中。

我發佈的header命令實際上是正確的。可悲的是我發佈了我的vcard附件主體修正。所以這個問題已經有了我的答案。 ::邦克::

此行是正確的。

$body .= "filename=\"".$filename."\"" . "\r\n\n"; 
$body .= $attachment . "\n\n"; 

這就是我的實際情況。

$body .= "filename=\"".$filename."\"" . "\r\n"; 

這解釋了爲什麼我有一個0字節的附件。

而且,只是爲了顯示實際工作的片段:

//File Manipulation 
     $file = fopen($fullpath,'rb'); 
     $pdfdata = fread($file, filesize($fullpath)); 
     fclose($file); 

    //Testing 
     // echo $fullpath . "<br />\n"; 
     //echo "Filename: " .$filename . "<br />\n"; 
     //echo "Filesize: " .filesize($fullpath). "<br />\n"; 
     // echo "String Length: " .strlen($pdfdata). "<br />\n"; 
     //var_dump($pdfdata); // Don't remove this (as a comment) again. LOL 
     // echo "Probable Errors for file_get_contents<br />\n"; 
     //$data = file_get_contents($pdfdata, true); 

     $attachment = chunk_split(base64_encode($pdfdata)); 
     //$attachment = chunk_split(base64_encode(file_get_contents($pdfdata))); 

對不起,如果我浪費任何人的時間。