简体   繁体   中英

Why download file dialog can not appear during php downloading zip file

In my project, I want to download xxx.zip file.

I have checked my apache server file:

/usr/local/apache2/conf/httpd.conf

The MIME of this file has only two AddType:

AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz

The two types can not support zip, So blow of them i have added:

AddType application/zip .zip

And then, I restarted httpd service successfully:

service httpd restart

And I run the php download :

<?php 
$file_name="xxx.zip"; 
header("Cache-Control: no-cache, must-revalidate");
header("Expires: 0");
header("Content-Disposition: attachment; filename=".basename($file_name));
header("Content-Length:".filesize($file_name));
header("Content-Type:application/zip");
readfile($file_name);
exit;
?>

But unlucky, my brower open content of xxx.zip directly, and there are messcy code instead of download dialog in my web site:

 @ӓ�����'t��    :�>]x�I�1W`� ���:�V2g�z����m^ ��Ӄ&8�E���l�:�kؖb��uƘ�z���qD���w�4��p��^&���s�����i/ݽ�mUH�F�x[`�:��#Nr��+|�n�i����ό�U#�}=!j�D�4�}=�'ujc�`��}hl�cr�"��T񼪚6���Λb��"/  ���k�vo$%��x/y���Q�����n�����Y�

Why download file dialog can not appear? My linux server, browser and DB use utf8 set. Who can help me ? I have tried other file format, eg:jpeg,png, they are all opening content directly not download file dialog.

Enter your code before any other output... in my case I created a separate page .(download.php?file=file_id_goes_here) and then call the file from files.php. so no other output before my download...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM