Unable to browse the site while downloading a file using php - php

I used this code for downloading files
$file='test.mp3';
$download_rate = 50; //50 kb/s
if(file_exists($file) && is_file($file))
{
header('Cache-control: private');
header('Content-Type: application/octet-stream');
header('Content-Length: '.filesize($file));
header('Content-Disposition: filename='.$file);
flush();
$file = fopen($file, "r");
while(!feof($file))
{
// send the current file part to the browser
print fread($file, round($download_rate * 1024));
// flush the content to the browser
flush();
// sleep one second
sleep(1);
}
fclose($file);
}
else {
echo 'File Not Found';
}
but while downloading the file cannot browse the site till the download completed. this happened with IE and Firefox
Any answers?

Only time I know this happens is when you have sessions which have not been written.
I can't see any sessions here so I'm not sure what is causing it.
However, most php download file scripts are used to check for logins so I'm guessing this is the case.
if you do have sessions, try session_write_close();

Related

downloading large zip files to the browser in php, chunking

I am trying to download large zip files (800MB to 1GB) containing audio files to the browser. As I have seen so far, chunking seems to be the most popular approach, but I am having zero luck. The code I have been working with is
$filename = $_SERVER['DOCUMENT_ROOT'] . $filepath;
$download_rate = 5000;
$progress = 0;
if (file_exists($filename)) {
header('Cache-control: private');
header('Content-Type: application/octet-stream');
header('Content-Length: '.filesize($filename));
header('Content-Disposition: filename='.basename($filename));
flush();
$file = fopen($filename, "r");
while(!feof($file)) {
// send the current file part to the browser
print fread($file, round($download_rate * 1024));
// flush the content to the browser
flush();
// sleep one second
sleep(1);
}
fclose($file);
} else echo 'File does not exist!';
This works for a wide variety of file types and sizes up to a certain point -- I have no problem downloading typical PDFs, etc. But the browser just spins and spins when I try to download large zip files, and eventually dies (around 2 minutes). I am really needing some help here. I've tried a number of code variants for chucking, but something always seems to die and I'm not sure what I'm doing wrong. Perhaps there's a better approach to chunking?
Consider change change for readfile will not present any memory issues, your lost download rate and progress show in browser. your limit download rate in .httaccess.
$filename = $_SERVER['DOCUMENT_ROOT'] . $filepath;
if (file_exists($filename)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: '.filesize($filename));
readfile($filename);
} else echo 'File does not exist!';
https://www.php.net/manual/en/function.readfile.php

php download limit zip damage when set file size

Zip damaged after set file size header
I have this code to limit download speed in php :
function readfile_chunked($filename, $retbytes = TRUE) {
$download_rate = 85; global $filename ;
// send headers
header('Cache-control: private');
header('Content-Type: application/octet-stream');
header('Content-Length: '.filesize($filename));
header('Content-Disposition: filename=file.zip');
// flush content
flush();
// open file stream
$file = fopen($filename, "r");
while(!feof($file)) {
// send the current file part to the browser
print fread($file, round($download_rate * 1024));
// flush the content to the browser
flush();
// sleep one second
usleep(200);
}
}
When i set header for content length like this:
header('Content-Length: '.filesize($filename));
and open the downloaded file show me an alert that file is damaged . Not just the zip files i try on jpg file and its the same .
BUT WHEN I REMOVE THE FILE SIZE HEADER THE FILE OR IMAGE OPEN WITHOUT ANY ERROR
I #solved the problem by adding ob_clean() in the begin of function. I was trying to solve it for 3 months This need a party ;)
function party(forVisitors){
Drink();
Play();
HaveFun();
GoHome();
}

Problems to reproduce Weight Video buffering php on Chrome

I have problems to reproduce a weight video (400mb or more) In Chrome. In Opera and Mozilla Firefox Works.
During the "streaming", I cant navigatintg the site, always is "Charging".
My script is:
http://codesamplez.com/programming/php-html5-video-streaming-tutorial
I tried to increment the buffering, but not works.
The videos are about the root folder and I have to access with HEADER.
I have problems with the script to download videos too, I cant navigatintg the site, always is "Charging" :
$local_file = $url;
$download_file = $url;
// set the download rate limit (=> 20,5 kb/s)
$download_rate = 140;
if(file_exists($local_file) && is_file($local_file))
{
header('Cache-control: private');
header('Content-Type: application/force-download');
header('Content-Length: '.filesize($local_file));
header('Content-Disposition: filename='.basename($download_file));
flush();
$file = fopen($local_file, "r");
while(!feof($file))
{
// send the current file part to the browser
print fread($file, round($download_rate * 1024));
// flush the content to the browser
flush();
// sleep one second
sleep(1);
}
fclose($file);}
}
Thx you.

Download speed limit script doesn't work

I've find a script here which permit to limit the download speed of a file.
// The file that will be sent to the user
$your_file = 'file.zip';
// Rename the file name
$new_file = 'new-filename.zip';
// Set the download speed limit (70 kb/s)
$download_speed = 70;
if(file_exists($myl_file) && is_file($my_file)) {
// Headers
header('Cache-control: private');
header('Content-Type: application/octet-stream');
header('Content-Length: '.filesize($my_file));
header('Content-Disposition: filename='.$new_file);
// Flush the content
flush();
// File stream
$file = fopen($my_file, "r");
while (!feof($file)) {
// Send the current part of the file to the browser
echo fread($file, round($download_speed* 1024));
// Flush the content to the browser
flush();
// Sleep one second
sleep(1);
}
// Close file stream
fclose($file);
}
else {
die('Error: The file '.$my_file.' does not exist!');
}
The problem is that the speed limit doesn't works (but I can download the file) I'm always between 1500-2000 kb/s when I change the $download_speed value. I've test it on wamp and on a webserver but impossible to make it work. Any ideas? Thanks you.

How to support IDM without allow request the page two times

I got this error ,as the above image show, while trying to download a file by IDM from my site. the limit of requesting this page one time only after entering Captcha Code and if you request the page again must enter the captcha code again.
How to support IDM in this case
The download code as the following
$file='test.mp3';
$download_rate = 50; //50 kb/s
if(file_exists($file) && is_file($file))
{
header('Cache-control: private');
header('Content-Type: application/octet-stream');
header('Content-Length: '.filesize($file));
header('Content-Disposition: filename='.$file);
flush();
$file = fopen($file, "r");
while(!feof($file))
{
// send the current file part to the browser
print fread($file, round($download_rate * 1024));
// flush the content to the browser
flush();
// sleep one second
sleep(1);
}
fclose($file);
}
else {
echo 'File Not Found';
}
Use xsendfile. A mod for apache
https://tn123.org/mod_xsendfile/

Categories