【发布时间】: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);
}
}
保存函数前的正则表达式转换附件文件中经常出现的特殊字符。
我可以在这里改进什么?
【问题讨论】: