title: how can i download all files attachments in php?
body: <p>i'm trying for my program to download all file attachments, but for some reason it doesn't download all of them only downloads a certain file attachment. This is the code:</p>
  
  <pre><code>&lt;?php
  
  set_time_limit(3000);
  $hostname = '{someoutlookemail.outlook.com:993/imap/ssl}INBOX';
  $username = 'someoutlookemail@outlook.com';
  $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-&gt;parts) &amp;&amp; count($structure-&gt;parts)) 
          {
              for($i = 0; $i &lt; count($structure-&gt;parts); $i++) 
              {
                  $attachments[$i] = array(
                      'is_attachment' =&gt; false,
                      'filename' =&gt; '',
                      'name' =&gt; '',
                      'attachment' =&gt; ''
                  );
  
                  if($structure-&gt;parts[$i]-&gt;ifdparameters) 
                  {
                      foreach($structure-&gt;parts[$i]-&gt;dparameters as $object) 
                      {
                          if(strtolower($object-&gt;attribute) == 'filename') 
                          {
                              $attachments[$i]['is_attachment'] = true;
                              $attachments[$i]['filename'] = $object-&gt;value;
                          }
                      }
                  }
  
                  if($structure-&gt;parts[$i]-&gt;ifparameters) 
                  {
                      foreach($structure-&gt;parts[$i]-&gt;parameters as $object) 
                      {
                          if(strtolower($object-&gt;attribute) == 'name') 
                          {
                              $attachments[$i]['is_attachment'] = true;
                              $attachments[$i]['name'] = $object-&gt;value;
                          }
                      }
                  }
  
                  if($attachments[$i]['is_attachment']) 
                  {
                      $attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i+1);
  
                      /* 3 = BASE64 encoding */
                      if($structure-&gt;parts[$i]-&gt;encoding == 3) 
                      { 
                          $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
                      }
                      /* 4 = QUOTED-PRINTABLE encoding */
                      elseif($structure-&gt;parts[$i]-&gt;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++ &gt;= $max_emails) break;
  
  
          /* output the email header information */
          $output.= '&lt;p&gt;&lt;div class="toggler '.($overview[0]-&gt;seen ? 'read' : 'unread').'"&gt;';
          $output.= '&lt;p&gt;Subject: &lt;span class="subject"&gt;'.$overview[0]-&gt;subject.'&lt;/span&gt; ';
          $output.= '&lt;p&gt;From: &lt;span class="from"&gt;'.$overview[0]-&gt;from.'&lt;/span&gt;';
          $output.= '&lt;p&gt;Date: &lt;span class="date"&gt;on '.$overview[0]-&gt;date.'&lt;/span&gt;';
          $output.= '&lt;/div&gt;';
  
          /* output the email body */
          $output.= '&lt;p&gt;Message:&lt;div class="body"&gt;'.$message.'';
          $output.= '&lt;p&gt;attachment:'.$filename.'';
          $output.= '&lt;table&gt;&lt;tr&gt;&lt;hr size="1" width="100%%" noshade color="black" &gt;&lt;/tr&gt;&lt;/table&gt;';
  
  
      }
  
      echo $output;
  }
  
  /* close the connection */
  imap_close($inbox);
  ?&gt;
  </code></pre>
  
  <p>whats wrong ? i tried to check all of my code and i dont understand why it only downloads some, i'm downloading the same type of file for now (.xml), one of them downloads and the other doesn't, the one who downloads has 1mb size and the other one 600kb, please help, i'have searched everywhere, and i can't find a solution.</p>