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.