2017-06-28 88 views
1

我想我的程序下載所有文件附件,但由於某種原因,它不下載所有文件附件。這是代碼:我該如何下載php中的所有文件附件?

<?php 

set_time_limit(3000); 
$hostname = '{someoutlookemail.outlook.com:993/imap/ssl}INBOX'; 
$username = '[email protected]'; 
$password = 'apass'; 

/* try to connect */ 
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error()); 

/* grab emails */ 
$emails = imap_search($inbox,'ALL'); 


$max_emails = 16; 

/* if emails are returned, cycle through each... */ 
if($emails) { 

    $count = 1; 

    /* begin output var */ 
    $output = ''; 

    /* put the newest emails on top */ 
    rsort($emails); 

    /* for every email... */ 
    foreach($emails as $email_number) { 

     /* get information specific to this email */ 
     $overview = imap_fetch_overview($inbox,$email_number,0); 
     $message = imap_fetchbody($inbox,$email_number,1.1); 
     $structure = imap_fetchstructure($inbox,$email_number); 

     $attachments = array(); 

     /* if any attachments found... */ 
     if(isset($structure->parts) && count($structure->parts)) 
     { 
      for($i = 0; $i < count($structure->parts); $i++) 
      { 
       $attachments[$i] = array(
        'is_attachment' => false, 
        'filename' => '', 
        'name' => '', 
        'attachment' => '' 
       ); 

       if($structure->parts[$i]->ifdparameters) 
       { 
        foreach($structure->parts[$i]->dparameters as $object) 
        { 
         if(strtolower($object->attribute) == 'filename') 
         { 
          $attachments[$i]['is_attachment'] = true; 
          $attachments[$i]['filename'] = $object->value; 
         } 
        } 
       } 

       if($structure->parts[$i]->ifparameters) 
       { 
        foreach($structure->parts[$i]->parameters as $object) 
        { 
         if(strtolower($object->attribute) == 'name') 
         { 
          $attachments[$i]['is_attachment'] = true; 
          $attachments[$i]['name'] = $object->value; 
         } 
        } 
       } 

       if($attachments[$i]['is_attachment']) 
       { 
        $attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i+1); 

        /* 3 = BASE64 encoding */ 
        if($structure->parts[$i]->encoding == 3) 
        { 
         $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']); 
        } 
        /* 4 = QUOTED-PRINTABLE encoding */ 
        elseif($structure->parts[$i]->encoding == 4) 
        { 
         $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']); 
        } 
       } 
      } 
     } 

     /* 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. 
       */ 
       $fp = fopen("./" . $email_number . "-" . $filename, "w+"); 
       fwrite($fp, $attachment['attachment']); 
       fclose($fp); 
      } 

     } 

     if($count++ >= $max_emails) break; 


     /* output the email header information */ 
     $output.= '<p><div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">'; 
     $output.= '<p>Subject: <span class="subject">'.$overview[0]->subject.'</span> '; 
     $output.= '<p>From: <span class="from">'.$overview[0]->from.'</span>'; 
     $output.= '<p>Date: <span class="date">on '.$overview[0]->date.'</span>'; 
     $output.= '</div>'; 

     /* output the email body */ 
     $output.= '<p>Message:<div class="body">'.$message.''; 
     $output.= '<p>attachment:'.$filename.''; 
     $output.= '<table><tr><hr size="1" width="100%%" noshade color="black" ></tr></table>'; 


    } 

    echo $output; 
} 

/* close the connection */ 
imap_close($inbox); 
?> 

怎麼了?我試圖檢查我所有的代碼,我不明白爲什麼它只下載一些,我現在下載相同類型的文件(.xml),其中一個下載,另一個沒有,下載的那個1mb大小和另一個600kb,請幫助,我到處搜索,我找不到解決方案。

+0

你試過 if(empty($ filename))$ filename = $ i.time()。修改 「.dat」; 其中$ i是一個增量編號,有時它只需要不到一秒鐘的時間來保存文件,因此您正在覆蓋它們 –

+0

我改變它就像你說的,但它仍然不下載所有文件,它下載某種類型的文件總是,如果我有兩個相同的文件,它下載它,但除了那個不同的,它不會下載... – Cactos

+0

[在PHP中隨附IMAP目錄下載附件,隨機作品]( https://stackoverflow.com/questions/2649579/downloading-attachments-to-directory-with-imap-in-php-randomly-works) – LuFFy

回答

0

你的腳本工作正常,我所有的文件正在下載。唯一的問題是沒有正確顯示,當你回顯輸出時,你應該迭代附件。像這樣的東西

<?php 

set_time_limit(3000); 


/* try to connect */ 


/* if emails are returned, cycle through each... */ 
if($emails) { 

    $count = 1; 

    /* begin output var */ 
    $output = ''; 

    /* put the newest emails on top */ 
    rsort($emails); 

    /* for every email... */ 
    foreach($emails as $email_number) { 

     /* get information specific to this email */ 
     $overview = imap_fetch_overview($inbox,$email_number,0); 
     $message = imap_fetchbody($inbox,$email_number,1.1); 
     $structure = imap_fetchstructure($inbox,$email_number); 

     $attachments = array(); 

     /* if any attachments found... */ 
     if(isset($structure->parts) && count($structure->parts)) 
     { 
      for($i = 0; $i < count($structure->parts); $i++) 
      { 
       $attachments[$i] = array(
        'is_attachment' => false, 
        'filename' => '', 
        'name' => '', 
        'attachment' => '' 
       ); 

       if($structure->parts[$i]->ifdparameters) 
       { 
        foreach($structure->parts[$i]->dparameters as $object) 
        { 
         if(strtolower($object->attribute) == 'filename') 
         { 
          $attachments[$i]['is_attachment'] = true; 
          $attachments[$i]['filename'] = $object->value; 
         } 
        } 
       } 

       if($structure->parts[$i]->ifparameters) 
       { 
        foreach($structure->parts[$i]->parameters as $object) 
        { 
         if(strtolower($object->attribute) == 'name') 
         { 
          $attachments[$i]['is_attachment'] = true; 
          $attachments[$i]['name'] = $object->value; 
         } 
        } 
       } 

       if($attachments[$i]['is_attachment']) 
       { 
        $attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i+1); 

        /* 3 = BASE64 encoding */ 
        if($structure->parts[$i]->encoding == 3) 
        { 
         $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']); 
        } 
        /* 4 = QUOTED-PRINTABLE encoding */ 
        elseif($structure->parts[$i]->encoding == 4) 
        { 
         $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']); 
        } 
       } 
      } 
     } 

     /* iterate through each attachment and save it */ 
     foreach($attachments as $key=>$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. 
       */ 
       $fp = fopen("./" . $email_number . "-" . $filename, "w+"); 
       fwrite($fp, $attachment['attachment']); 
       fclose($fp); 

       $attachments[$key]['filename'] = $filename; 
      } 

     } 

     if($count++ >= $max_emails) break; 


     /* output the email header information */ 
     $output.= '<p><div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">'; 
     $output.= '<p>Subject: <span class="subject">'.$overview[0]->subject.'</span> '; 
     $output.= '<p>From: <span class="from">'.$overview[0]->from.'</span>'; 
     $output.= '<p>Date: <span class="date">on '.$overview[0]->date.'</span>'; 
     $output.= '</div>'; 

     /* output the email body */ 
     $output.= '<p>Message:<div class="body">'.$message.''; 
     foreach ($attachments as $attachment) { 
     $output.= '<p>attachment:'.$attachment['filename'].''; 
     } 
     $output.= '<table><tr><hr size="1" width="100%%" noshade color="black" ></tr></table>'; 


    } 

    echo $output; 
} 

/* close the connection */ 
imap_close($inbox); 
?> 
+0

我改變了附件的輸出,它仍然一直給我同樣的錯誤它說: fopen(./1- =?iso-8859-1?Q?513178643 = 5F2017-05-01 = 5F2017-05-31 = 5FGlobal = 5FSaft = 5FFatu?=?iso-8859-1 ?Q?ra = E7 = E3o.XML?=):未能打開流 – Cactos

+0

也即使我一次下載一個,它仍然只下載一直工作的相同文件,即使我刪除所有附件並一個一個下載,它仍然只下載該特定的一個...我不明白爲什麼 – Cactos