簡體   English   中英

警告,導致無法正確讀取file_get_content

[英]Warnings preventing file_get_content from reading properly

在附加附件時,我收到一條警告消息,阻止我將現有的pdf文件附加到我的電子郵件中。 我知道這不是我的標頭,因為腳本已成功附加了由字符串早期生成的vcard。 由於我不編輯文件,因此不需要TDPDF和FPDF。 (盡管不是100%)這是我一直在使用的代碼。 我已經包含了我的輸出測試行和注釋。

//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)));

輸出:

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個不同的變量中找到:$ pdfdata和$ filesize。 他們匹配。 我將提到服務器(由於字符集)已截斷了響應。 這就是為什么我開始檢查長度的原因。

最后,以防萬一它可能是我的標頭,因為我能夠成功發送一個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()的“二進制安全”描述,但這不應該導致腳本失敗。 應該是?

解決該問題的任何幫助將不勝感激。

好。 我修好了它。 奇怪的是,它出現在我的標題中。

我發布的標頭命令實際上是正確的。 可悲的是,我發布了vcard附件的附件。 所以這個問題已經有了我的答案。 :: bonk ::

這行是正確的。

$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)));

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM