2017-06-28 1 views
1

Je suis en train d'essayer pour mon programme de télécharger toutes les pièces jointes, mais pour une raison quelconque, il ne télécharge pas tous seulement télécharge une pièce jointe certaine. C'est le code:comment puis-je télécharger tous les fichiers joints en 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); 
?> 

quoi de mal? J'ai essayé de vérifier tout mon code et je ne comprends pas pourquoi il n'en télécharge que quelques-uns, je télécharge le même type de fichier pour le moment (.xml), l'un d'eux télécharge et l'autre pas, celui qui télécharge Taille 1mb et l'autre 600kb, s'il vous plaît aider, j'ai cherché partout, et je ne peux pas trouver une solution.

+0

Avez-vous essayé si (vide (nom de $)) $ filename = i.time $(). ".dat"; où $ i est un nombre incrémental, parfois il faut moins d'une seconde pour enregistrer les fichiers, donc vous les écrasez –

+0

je l'ai changé comme vous l'avez dit, mais il ne télécharge toujours pas tous les fichiers, il télécharge un certain type de fichier toujours, et si j'ai deux de ce même fichier, il le télécharge, mais différents à part celui-là, il ne télécharge pas ... – Cactos

+0

Possible copie de [Téléchargement des pièces jointes au répertoire avec IMAP en PHP, fonctionne aléatoirement] (https://stackoverflow.com/questions/2649579/downloading-attachments-to-directory-with-imap-in-php-randomly-works) – LuFFy

Répondre

0

Votre script fonctionne très bien pour moi, tous les fichiers sont en cours de téléchargement. Le seul problème est que cela ne s'affiche pas correctement, vous devriez itérer sur les pièces jointes, lorsque vous faites écho à la sortie. Quelque chose comme ça

<?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

J'ai changé la sortie sur les pièces jointes, et il continue à me donner la même erreur , il est dit: fopen (./ 1 - = iso-8859-1? Q? 513178643 = 5F2017-05-01 = 5F2017-05-31 = 5FGlobal = 5FSaft = 5FFatu? = =? iso-8859-1 ? Q? Ra = E7 = E3o.XML? =): N'a pas pu ouvrir le flux – Cactos

+0

Aussi même si je télécharge un à la fois, il télécharge toujours seulement le même fichier qui a toujours fonctionné, même si je supprime toutes les pièces jointes et télécharge un par un, il télécharge seulement celui spécifique ... je ne comprends pas pourquoi – Cactos