0

I am working on my PHP to store the file in an array. Now I would like to download the file from the array.

When I try this:

if($attachments[$attach_id])
{
    $filename = $attachments[$attach_id]['name'];
    $attachment = $attachments[$attach_id]['attachment'];
    header("Content-Type: application/octet-stream");
    header("Content-Transfer-Encoding: Binary");
    header("Content-disposition: attachment; filename=".basename($attachment)); 
    echo readfile($attachment);
}

It will not extract the file from the array to let me to download the file.

Here is the full code:

<?php

require_once "Mail.php";
require_once('Mail/IMAPv2.php');

$username = 'username';
$password = 'password';
$mailserver = '{imap.domain.com:993/imap/ssl/novalidate-cert}INBOX';
$mailbox = imap_open($mailserver, $username, $password) or die("Can't connect: " . imap_last_error());
$key = "key";
$email_number = "279";
$attach_id = '1';
echo "hello!";

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

$message = imap_fetchbody($mailbox, $email_number, 2);

/* get mail structure */
$structure = imap_fetchstructure($mailbox, $email_number);
$attachments = array();
$attachment_number = 0;


if(isset($structure->parts) && count($structure->parts)) {

    for($i = 0; $i < count($structure->parts); $i++) {

        if (($structure->parts[$i]->ifdisposition) && ($structure->parts[$i]->disposition == 'attachment')) {

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


                    if($attachments[$attachment_number]['is_attachment']) {
                        $attachments[$attachment_number]['attachment'] = imap_fetchbody($mailbox, $email_number, 2);


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

                            $attachments[$attachment_number]['attachment'] = quoted_printable_decode($attachments[$attachment_number]['attachment']);
                        }
                    }
                }
            }
            $attachment_number++;
        }
    }
}


if($attachments[$attach_id])
{
    $filename = $attachments[$attach_id]['name'];
    $attachment = $attachments[$attach_id]['attachment'];
    header("Content-Type: application/octet-stream");
    header("Content-Transfer-Encoding: Binary");
    header("Content-disposition: attachment; filename=".basename($attachment)); 
    echo readfile($attachment);
}

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

?>

How do you download the file from the array when I store them?

Do I have to output them first or if there is a way that if I could be able to extract and then download it?

I have not store the files on the server as I want to download the attachment from the email using the id.

Thank you.

Robert Jones
  • 390
  • 3
  • 18
  • you got any errors? – icy Jun 30 '19 at 14:52
  • @andreah No I dont have any error. I have got the contents display in the php but i cant download it. Any idea how i can download it? – Robert Jones Jun 30 '19 at 15:09
  • like this: `Rar!�������V(�� �$��rmreport.php �_ږN��ͷ"6C33�`T�\+c�7o���C����#t_A4�Hȑ*����/x�[���6�8��V�n�)r����?x}�=������.���A�F+'1(�Q҇���VG]�`�x�֚����A��]F!g�v��6��sX֢�3LiF�a`(M�/�8� ��)a�P��{���]᰿�t�����{�@){f��.X%͓ L��`�x��mE�M&ڙw��9B���zN�-�6�v�����F��~f���/�N��Jܬ��;#���D�[)�,r�RlC$�0]��<�-�գN�o��ņԏq�u&��c�%����K��U���40j+��-C���Ax�>��˯G�n�N'� K��w}�y���B$�7z�?����7y����� ���Ʋ.�*��0-��=�,���^3-_ %&C�eZ/`ֈ� 2O���$�go �l吜ao.�rR�~ l7Z��{9w-b ;���r�?߫����{L:��Y��g;�������� 囜�}send_mail.php` – Robert Jones Jun 30 '19 at 15:18
  • 1
    i'm not able to test it, but try to change `header("Content-Type: application/octet-stream")` with `header("Content-Type: ".mime_content_type($attachment))` – icy Jun 30 '19 at 21:42
  • 1
    let me know if it works please, thanks – icy Jul 01 '19 at 15:12
  • @andreah yes it does work. thank you. – Robert Jones Jul 01 '19 at 17:54
  • very good! I've posted my answer for anyone need this! – icy Jul 01 '19 at 19:42
  • can you accept my answer if it helped? thank you. – icy Jul 02 '19 at 07:41

1 Answers1

1

Replace:

header("Content-Type: application/octet-stream");

with:

 header("Content-Type: ".mime_content_type($attachment));

This is because Content-Type: application/octet-stream. Use mime_content_type() to find the right mime-type.

For more information about content-type: https://stackoverflow.com/a/20509354/1403785

icy
  • 1,468
  • 3
  • 16
  • 36