PHP从数组下载该文件
我正在使用PHP将文件存储在数组中。现在我想从数组中下载该文件 当我尝试这个:PHP从数组下载该文件,php,Php,我正在使用PHP将文件存储在数组中。现在我想从数组中下载该文件 当我尝试这个: if($attachments[$attach_id]) { $filename = $attachments[$attach_id]['name']; $attachment = $attachments[$attach_id]['attachment']; header("Content-Type: application/octet-stream"); header("Conte
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);
}
它不会从阵列中提取文件以让我下载该文件
以下是完整的代码:
<?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);
?>
当我存储文件时,如何从阵列下载文件
我是否必须先输出它们,或者是否有一种方法可以提取并下载它们
我没有将文件存储在服务器上,因为我想使用id从电子邮件下载附件
谢谢。替换:
header("Content-Type: application/octet-stream");
与:
这是因为内容类型:应用程序/八位字节流。使用mime\u内容\u类型查找正确的mime类型
有关内容类型的详细信息:
你有什么错误吗?@andreah没有,我没有任何错误。我已经在php中显示了内容,但我无法下载它。知道怎么下载吗?像这样:Rar!�������v�� �$��rmreport.php�_ږN��ͷ6C33�T�\+C�7o���C����图A4�Hȑ*����/x�[���6.�8.��v�N�R����?x}�=������.���A.�F+'1�Q҇���VG]��x�֚����A.��]FG�v��6.��sX֢�3LiF�是�/�8.� ��A.�P��{���]᰿�T�����{�@{f��.X%͓L���x��我�M&ڙw��9B���锌�-�6.�v�����F��~F���/�N��Jܬ��;���D�[�,R�RlC$�0]����˯G�N�不�K��w}�Y���B$�7z�?����7y����� ���Ʋ.�*��0-��=�,���^3-u%&C�eZ/ֈ� 20���$�去�L吜敖。�rR�~l7Z��{9w-b;���R�?߫����{L:��Y��G�������� 囜�}send_mail.php`我无法测试它,但尝试将headerContent类型更改为:application/octet stream,headerContent类型为:.mime_content_Type$Attachment请告诉我它是否有效,谢谢
header("Content-Type: ".mime_content_type($attachment));