Idiot-proof, cross-browser force download in PHP

后端 未结 7 1295
借酒劲吻你
借酒劲吻你 2020-12-16 05:40

I\'m using forced download to download mostly zips and mp3s on site i did (http://pr1pad.kissyour.net) - to track downloads in google analytics, in database and to hide real

7条回答
  •  南笙
    南笙 (楼主)
    2020-12-16 06:29

    There's one thing I find weird: You are calling ob_end_flush() at the start of the function. This actually cleans the output buffer, but it also outputs everything to the client first (I assume including Content-Headers set by CodeIgniter). Change the call to ob_end_clean(), it clears the buffer and discards it. This will give you a clean start for generating your own headers.

    Another tip:

    Instead of reading the file as a stream and passing it on block-wise, you could give this function a try:

    // ...
    if (file_exists("dir-with-files/$filename")) {
       readfile($file);
    }
    

    This takes care of nearly everything.

提交回复
热议问题