在附加附件时,我收到一条警告消息,阻止我将现有的 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() 的“二进制安全”描述,但这不应该导致脚本失败。应该是?
任何解决此问题的帮助将不胜感激。