Page 1 of 1

Facing problem in downloading big files

Posted: Sun Nov 23, 2008 7:32 am
by ummarbhutta
Hi!
I am working on a simple project which is some how related to download a big file (80MB), while hiding the actual physical location of file from the user, I am doing this by writing a simple php file, which takes ID of file as argument and then from database picks the name of file and then simply write the file to input stream of client browser.. All the things are working fine but problem is that while downloading the big file, if user wants to open some other link of my website it doesn't work untill the download is complete.... Or He/She opens another browser window and start surffing from there....which is unacceptable for me.... It seems that server and client are working on single thread model which is obviously not true.. here is the code snippet of my download.php file..

Code: Select all

 
<?php
    $fSize = filesize("myvideos/videos/testvideo.flv");
    header('Content-transfer-encoding: binary');
             header('Content-Type: video/x-flv');
    header('Content-Length: '.$fSize);
    header('Content-Disposition: attachment; '.'filename='testvideo.flv'");
    header ("Pragma: public");
    header ("Expires: 0");
    header ("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header ("Cache-Control: private");
 
    ob_clean();
    ob_end_flush();
 
    readfile("myvideos/videos/testvideo.flv"); /*Write file to out put stream*/
?>
 
the readfile function I am using is putting whole of 80MB file in one attempt to input stream of client browser, I know that the problem is due to this function, but how to reolve it? Please let me know, if any one can help me as soon as possible, I would really appreciate it! I expect some help from PHP Gurus!
Regards
Bye
Ummar

Re: Facing problem in downloading big files

Posted: Sun Nov 23, 2008 8:05 am
by Hannes2k
Hi,
PHP Manual => readfile => Comments:
In response to flowbee@gmail.com --

When using the readfile_chunked function noted here with files larger than 10MB or so I am still having memory errors. It's because the writers have left out the all important flush() after each read. So this is the proper chunked readfile (which isn't really readfile at all, and should probably be crossposted to passthru(), fopen(), and popen() just so browsers can find this information):

<?php
function readfile_chunked($filename,$retbytes=true) {
$chunksize = 1*(1024*1024); // how many bytes per chunk
$buffer = '';
$cnt =0;
// $handle = fopen($filename, 'rb');
$handle = fopen($filename, 'rb');
if ($handle === false) {
return false;
}
while (!feof($handle)) {
$buffer = fread($handle, $chunksize);
echo $buffer;
ob_flush();
flush();
if ($retbytes) {
$cnt += strlen($buffer);
}
}
$status = fclose($handle);
if ($retbytes && $status) {
return $cnt; // return num. bytes delivered like readfile() does.
}
return $status;

}
?>

All I've added is a flush(); after the echo line. Be sure to include this!
I hope this helps you

Re: Facing problem in downloading big files

Posted: Sun Nov 23, 2008 9:50 am
by ummarbhutta
Thanks buddy! I was also thinking about such solution.. Ok let me check it first then I will let you know whether it works or not!.. anyway thanks for your time and help

Re: Facing problem in downloading big files

Posted: Sun Nov 23, 2008 11:31 am
by ummarbhutta
:cry: :cry: The problem is still there.. although I am sending file in chunks... It seems that browser gets busy in receiving the big .flv file being sent by the server and don't entertain any other request.. However i have tried another approach.. If I give the name of file in a hyperlink .. i.e. simply actual path to that file then it works fine.... If anyone has any other approach by using which I can hide the actual path of file without the above problem would really be very helpful for me..

Re: Facing problem in downloading big files

Posted: Sun Nov 23, 2008 6:13 pm
by kaisellgren
ummarbhutta wrote::cry: :cry: The problem is still there.. although I am sending file in chunks... It seems that browser gets busy in receiving the big .flv file being sent by the server and don't entertain any other request.. However i have tried another approach.. If I give the name of file in a hyperlink .. i.e. simply actual path to that file then it works fine.... If anyone has any other approach by using which I can hide the actual path of file without the above problem would really be very helpful for me..
I'm tired right now, but I got an idea.

Type the real path to your browser and let browser start the download. Before that, if using Firefox, get a plugin called Live HTTP Headers. It will show you the headers your browser & server send. Then use the same headers in your script and it should work fine.