我已经成功创建了一个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);
}
}
保存功能之前的正则表达式会转换通常出现在附件文件中的特殊字符。
我可以在这里改进什么?
答案 0 :(得分:0)
以下行存在问题......
$converted = utf8_decode(iconv_mime_decode($filename));
iconv_mime_decode函数解码标头。您正在传递文件名。您需要加载文件内容,然后将内容传递给该函数。
$contents = file_get_contents($filename);
$converted = utf8_decode(iconv_mime_decode($contents));