【问题标题】:php imap functions download corrupt filesphp imap 函数下载损坏的文件
【发布时间】:2016-09-03 07:41:24
【问题描述】:

我已经成功创建了一个 php 脚本,可以将特定的电子邮件附件下载到我的服务器。

但是,它们似乎都已损坏。 (下载的文件名正确,但服务器上的文件大小为 0 KB)

这是我用于下载的代码:

/* iterate through each attachment and save it */
foreach($attachments as $attachment){

  if($attachment['is_attachment'] == 1)
  {
    $filename = $attachment['name'];
    if(empty($filename)) $filename = $attachment['filename'];

    if(empty($filename)) $filename = time() . ".dat";

    /* prefix the email number to the filename in case two emails
       * have the attachment with the same file name.
    */
    $converted = utf8_decode(iconv_mime_decode($filename));
    $text = preg_replace('/[^a-zA-Z0-9_.]/', '_', $converted);

    $fp = fopen("att/".$text, "w+");
    fwrite($fp, $attachment['attachment']);
    fclose($fp);
  }
}

保存函数前的正则表达式转换附件文件中经常出现的特殊字符。

我可以在这里改进什么?

【问题讨论】:

    标签: php email imap


    【解决方案1】:

    下面这行有问题...

    $converted = utf8_decode(iconv_mime_decode($filename));
    

    iconv_mime_decode 函数对标头进行解码。您正在传递一个文件名。您需要加载文件内容,然后将内容传递给该函数。

    $contents = file_get_contents($filename);
    $converted = utf8_decode(iconv_mime_decode($contents));
    

    【讨论】:

    • 感谢您的回答,不确定您的确切意思...我认为这会弄乱文件名...
    • 基本上 iconv_mime_decode 解码文件的内容。你给它的文件名。您需要阅读文件并为其提供内容,而不是文件名。
    • 附件内容实际上不是 $filename 中的,而是 fwrite($fp, $attachment['attachment']); ...所以我认为这条线有问题
    猜你喜欢
    • 2015-07-16
    • 2014-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-23
    • 2012-12-05
    相关资源
    最近更新 更多