title: imap - get attached file
body: <p>How do I get the attached file from this email?</p>
  
  <p>This email is sent from an apple computer and the email struture is not like any other (surprise).. here the part with the disposition is one dimension deeper than else..</p>
  
  <p>The script works with every other email where the part with the file is in the first dimension, but not with this one</p>
  
  <p><code>$part-&gt;dparameters[0]-&gt;value</code> returns the file name, but <code>strlen($data)</code> returns <code>0</code></p>
  
  <h1>imap stream</h1>
  
  <pre><code>$structure = imap_fetchstructure($this-&gt;stream, $this-&gt;msgno);
  
  if(isset($structure-&gt;parts)){
      print_r($structure-&gt;parts);
      $this-&gt;parse_parts($structure-&gt;parts);
  }
  
  function parse_parts($parts){
      foreach($parts as $section =&gt; $part){
          if(isset($part-&gt;parts)){
  
              // some mails have one extra dimension
              $this-&gt;parse_parts($part-&gt;parts);
  
          }
          elseif(isset($part-&gt;disposition)){
              if(in_array(strtolower($part-&gt;disposition), array('attachment','inline'))){
                  $data = imap_fetchbody($this-&gt;stream, $this-&gt;msgno, $section+1);
                  echo $part-&gt;dparameters[0]-&gt;value.' '.strlen($data)."\n";
              }
          }
      }
  }
  </code></pre>
  
  <h1>print_r</h1>
  
  <pre><code>Array
  (
      [0] =&gt; stdClass Object
          (
              [type] =&gt; 0
              [encoding] =&gt; 0
              [ifsubtype] =&gt; 1
              [subtype] =&gt; PLAIN
              [ifdescription] =&gt; 0
              [ifid] =&gt; 0
              [lines] =&gt; 15
              [bytes] =&gt; 173
              [ifdisposition] =&gt; 0
              [ifdparameters] =&gt; 0
              [ifparameters] =&gt; 1
              [parameters] =&gt; Array
                  (
                      [0] =&gt; stdClass Object
                          (
                              [attribute] =&gt; CHARSET
                              [value] =&gt; us-ascii
                          )
  
                  )
  
          )
  
      [1] =&gt; stdClass Object
          (
              [type] =&gt; 1
              [encoding] =&gt; 0
              [ifsubtype] =&gt; 1
              [subtype] =&gt; MIXED
              [ifdescription] =&gt; 0
              [ifid] =&gt; 0
              [bytes] =&gt; 23420
              [ifdisposition] =&gt; 0
              [ifdparameters] =&gt; 0
              [ifparameters] =&gt; 1
              [parameters] =&gt; Array
                  (
                      [0] =&gt; stdClass Object
                          (
                              [attribute] =&gt; BOUNDARY
                              [value] =&gt; Apple-Mail=_800896E0-A9C9-456E-B063-79CED9DD4FD7
                          )
  
                  )
  
              [parts] =&gt; Array
                  (
                      [0] =&gt; stdClass Object
                          (
                              [type] =&gt; 0
                              [encoding] =&gt; 0
                              [ifsubtype] =&gt; 1
                              [subtype] =&gt; HTML
                              [ifdescription] =&gt; 0
                              [ifid] =&gt; 0
                              [bytes] =&gt; 136
                              [ifdisposition] =&gt; 0
                              [ifdparameters] =&gt; 0
                              [ifparameters] =&gt; 1
                              [parameters] =&gt; Array
                                  (
                                      [0] =&gt; stdClass Object
                                          (
                                              [attribute] =&gt; CHARSET
                                              [value] =&gt; us-ascii
                                          )
  
                                  )
  
                          )
  
                      [1] =&gt; stdClass Object
                          (
                              [type] =&gt; 3
                              [encoding] =&gt; 3
                              [ifsubtype] =&gt; 1
                              [subtype] =&gt; PDF
                              [ifdescription] =&gt; 0
                              [ifid] =&gt; 0
                              [bytes] =&gt; 17780
                              [ifdisposition] =&gt; 1
                              [disposition] =&gt; INLINE
                              [ifdparameters] =&gt; 1
                              [dparameters] =&gt; Array
                                  (
                                      [0] =&gt; stdClass Object
                                          (
                                              [attribute] =&gt; FILENAME
                                              [value] =&gt; 057 - LPJ - Stik og labels.pdf
                                          )
  
                                  )
  
                              [ifparameters] =&gt; 1
                              [parameters] =&gt; Array
                                  (
                                      [0] =&gt; stdClass Object
                                          (
                                              [attribute] =&gt; NAME
                                              [value] =&gt; 057 - LPJ - Stik og labels.pdf
                                          )
  
                                  )
  
                          )
  
                      [2] =&gt; stdClass Object
                          (
                              [type] =&gt; 0
                              [encoding] =&gt; 4
                              [ifsubtype] =&gt; 1
                              [subtype] =&gt; HTML
                              [ifdescription] =&gt; 0
                              [ifid] =&gt; 0
                              [lines] =&gt; 75
                              [bytes] =&gt; 4931
                              [ifdisposition] =&gt; 0
                              [ifdparameters] =&gt; 0
                              [ifparameters] =&gt; 1
                              [parameters] =&gt; Array
                                  (
                                      [0] =&gt; stdClass Object
                                          (
                                              [attribute] =&gt; CHARSET
                                              [value] =&gt; us-ascii
                                          )
  
                                  )
  
                          )
  
                  )
  
          )
  
  )
  </code></pre>