警告阻止file_get_content正确读取

时间:2012-09-06 03:10:44

标签: php email attachment

在附加附件时,我收到一条警告消息,阻止我将现有的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

它告诉我文件大小,可以在两个不同的变量中找到:$ pdfdata&amp; $文件大小。他们匹配。我会提到我截断的响应(由于charset)已经被服务器截断了。这就是我开始检查长度的原因。

最后,以防万一它可能成为我的标题,因为我能够成功发送一个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()&amp;的“二进制安全”描述。 fread(),但这不应该导致脚本失败。应该吗?

任何帮助解决这个问题都将非常感激。

1 个答案:

答案 0 :(得分:1)

确定。我修好了它。奇怪的是,它在我的标题中。

我发布的标题命令实际上是正确的。可悲的是,我发布了我的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)));

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