2012-09-06 46 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))); 

对不起,如果我浪费任何人的时间。