За последние 24 часа нас посетили 22856 программистов и 1224 робота. Сейчас ищут 802 программиста ...

Сохранение файлов

Тема в разделе "Сделайте за меня", создана пользователем seriousdark, 3 янв 2018.

  1. seriousdark

    seriousdark Новичок

    С нами с:
    3 янв 2018
    Сообщения:
    3
    Симпатии:
    0
    Здравствуйте,есть код для выгрузки вложений с почты, помогите исправить чтоб он выгружал не в папку с скриптом а заданную директорию ( /pochta ).
    П.С в пхп полный 0
    PHP:
    1. <?php
    2.  
    3.  
    4. $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
    5. $username = 'YOUR_GMAIL_USERNAME'; # например somebody@gmail.com
    6. $password = 'YOUR_GMAIL_PASSWORD';
    7.  
    8. $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
    9.  
    10. $emails = imap_search($inbox,'ALL');
    11. $max_emails = 16;
    12.  
    13. if($emails) {
    14.  
    15. $count = 1;
    16. rsort($emails);
    17. foreach($emails as $email_number)
    18. {
    19.  
    20. /* get information specific to this email */
    21. $overview = imap_fetch_overview($inbox,$email_number,0);
    22.  
    23. /* get mail message */
    24. $message = imap_fetchbody($inbox,$email_number,2);
    25.  
    26. /* get mail structure */
    27. $structure = imap_fetchstructure($inbox, $email_number);
    28.  
    29. $attachments = array();
    30.  
    31. /* if any attachments found... */
    32. if(isset($structure->parts) && count($structure->parts))
    33. {
    34. for($i = 0; $i < count($structure->parts); $i++)
    35. {
    36. $attachments[$i] = array(
    37. 'is_attachment' => false,
    38. 'filename' => '',
    39. 'name' => '',
    40. 'attachment' => ''
    41. );
    42.  
    43. if($structure->parts[$i]->ifdparameters)
    44. {
    45. foreach($structure->parts[$i]->dparameters as $object)
    46. {
    47. if(strtolower($object->attribute) == 'filename')
    48. {
    49. $attachments[$i]['is_attachment'] = true;
    50. $attachments[$i]['filename'] = $object->value;
    51. }
    52. }
    53. }
    54.  
    55. if($structure->parts[$i]->ifparameters)
    56. {
    57. foreach($structure->parts[$i]->parameters as $object)
    58. {
    59. if(strtolower($object->attribute) == 'name')
    60. {
    61. $attachments[$i]['is_attachment'] = true;
    62. $attachments[$i]['name'] = $object->value;
    63. }
    64. }
    65. }
    66.  
    67. if($attachments[$i]['is_attachment'])
    68. {
    69. $attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i+1);
    70.  
    71. /* 4 = QUOTED-PRINTABLE encoding */
    72. if($structure->parts[$i]->encoding == 3)
    73. {
    74. $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
    75. }
    76. /* 3 = BASE64 encoding */
    77. elseif($structure->parts[$i]->encoding == 4)
    78. {
    79. $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
    80. }
    81. }
    82. }
    83. }
    84.  
    85. /* iterate through each attachment and save it */
    86. foreach($attachments as $attachment)
    87. {
    88. if($attachment['is_attachment'] == 1)
    89. {
    90. $filename = $attachment['name'];
    91. if(empty($filename)) $filename = $attachment['filename'];
    92.  
    93. if(empty($filename)) $filename = time() . ".dat";
    94.  
    95. /* prefix the email number to the filename in case two emails
    96. * have the attachment with the same file name.
    97. */
    98. $fp = fopen($email_number . "-" . $filename, "w+");
    99. fwrite($fp, $attachment['attachment']);
    100. fclose($fp);
    101. }
    102.  
    103. }
    104.  
    105. if($count++ >= $max_emails) break;
    106. }
    107.  
    108. }
    109.  
    110. /* close the connection */
    111. imap_close($inbox);
    112.  
    113. echo "Done";
    114.  
    115. ?>
    <?php

    set_time_limit(3000);

    $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
    $username = 'YOUR_GMAIL_USERNAME'; # например somebody@gmail.com
    $password = 'YOUR_GMAIL_PASSWORD';

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

    $emails = imap_search($inbox,'ALL');
    $max_emails = 16;

    if($emails) {

    $count = 1;
    rsort($emails);
    foreach($emails as $email_number)
    {

    /* get information specific to this email */
    $overview = imap_fetch_overview($inbox,$email_number,0);

    /* get mail message */
    $message = imap_fetchbody($inbox,$email_number,2);

    /* get mail structure */
    $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);

    /* 4 = QUOTED-PRINTABLE encoding */
    if($structure->parts[$i]->encoding == 3)
    {
    $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
    }
    /* 3 = BASE64 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;
    }

    }

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

    echo "Done";

    ?>
     
    #1 seriousdark, 3 янв 2018
    Последнее редактирование модератором: 3 янв 2018
  2. MouseZver

    MouseZver Суперстар

    С нами с:
    1 апр 2013
    Сообщения:
    7.752
    Симпатии:
    1.322
    Адрес:
    Лень
    сколько платишь за изменение строки ?
     
  3. artoodetoo

    artoodetoo Суперстар
    Команда форума Модератор

    С нами с:
    11 июн 2010
    Сообщения:
    11.076
    Симпатии:
    1.237
    Адрес:
    там-сям
    будь так добр, отредактируй свой пост, помести код в ббкод
    [
    php][/php]
    --- Добавлено ---
    @seriousdark
    --- Добавлено ---
    @MouseZver халявщики платят только чёрной ненавистью
     
  4. MouseZver

    MouseZver Суперстар

    С нами с:
    1 апр 2013
    Сообщения:
    7.752
    Симпатии:
    1.322
    Адрес:
    Лень
    хочу вот царский опробовать в робин сдобине
    --- Добавлено ---
    а там еще пару салатиками накинуть и чаек