开发者

PHP forcing file download. Not showing what amount of space has been downloaded

开发者 https://www.devze.com 2022-12-16 13:47 出处:网络
I have a issue, I have a PHP script that compresses images in a Zip file and then forcing the zip file to download. Now my issue is it\'s not showing in IE how much space has been downloaded sofar. Eg

I have a issue, I have a PHP script that compresses images in a Zip file and then forcing the zip file to download. Now my issue is it's not showing in IE how much space has been downloaded sofar. Eg 2MB's out of 20MB's..... 15secs remaining. Firefox works perfect.

My code

           header("Content-Disposition: attachment; filename=" . urlencode($zipfile));
        header("Content-Type: application/x-zip-compressed");
        header("Content-Description: File Transfer");    
           header("Content-Transfer-Encoding: binary");
       开发者_运维技巧    header("Content-Length: " . filesize($filename));

            $fd = fopen($filename,'r');

            $content = fread($fd, filesize($filename));



            print $content;


The main issue, as noted in the comment, is that you are storing the whole file into memory prior to sending which may cause a very long wait depending on file size. What you would prefer to do is output the buffer as segments are read into memory, in 1024 byte blocks, for example.

You could try something more along the lines of:

if ($file = fopen($filename, 'rb')) {
    while(!feof($file)) {
        print(fread($file, 1024*8));
        flush();
    }
    fclose($file);
}

Which will not attempt to read the entire file prior to output, but rather output blocks of the file as they are read into the output buffer.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号