PHP ZIP file download

后端 未结 7 1280
一生所求
一生所求 2020-12-17 05:51

Here is a code which downloads attachment files from the an imap server. Almost all file types (pdf, doc, xls, etc) are being downloaded correctly, where as some zip files g

7条回答
  •  失恋的感觉
    2020-12-17 06:32

    You're very close to the solution! I found the way to solve this was to remove the Content-type header all together and let the browser figure out that this was a .zip file. Remove the Content-Type and it should work as needed.

    // Remove this line 
    header('Content-Type: '. $type .'/'. $subtype);
    

    In my console (Chrome) I was receiving a strange message(s) saying:

    Resource interpreted as Document but transferred with MIME type application/octet-stream:

    Resource interpreted as Document but transferred with MIME type application/zip:

    When I removed the MIME Type (Content-type header) the .zip stopped giving me an error and worked normally.

    The most basic download script for making the .zip to work looked like this at the end: (note: You can still add more headers for caching and such)

    
    

    Edited

    Your .zip's will also run into problems if the browser is reading extra spaces. You can prevent the extra spaces at the end of the readfile() by adding exit(); but you'll want to make sure you don't have any spaces at the start of the file. This is particularly common when dealing with frameworks (especially your own). A little trick: If you include all the functions, configs and classes before your buffer starts outputting HTML, view your source code. At the start there will be a space before the .

提交回复
热议问题