2016-09-03 9 views
0

私のサーバー上の特定の電子メールの添付ファイルをダウンロードするPHPスクリプトが正常に作成されました。php imap関数は破損したファイルをダウンロードします

ただし、それらはすべて壊れているようです。が

/* 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)); 
+0

おかげで、あなたは何を意味するかわからないが、正確に...私は、これはファイル名... – mirzahat

+0

は基本的にiconv_mime_decodeは、ファイルの内容を解読を台無しだと思います。あなたはファイル名を与えています。ファイルを読み込み、ファイル名ではなく内容を渡す必要があります。 – RCrowt

+0

添付ファイルの内容は$ filename内ではなく、fwrite($ fp、$ attachment ['attachment'])行にあります。 ...この行は問題だと私は思います – mirzahat

関連する問題