Php File Download Script
if you need to limit download rate, use this code
<?php
$local_file = 'file.zip';
$download_file = 'name.zip';
// set the download rate limit (=> 20,5 kb/s)
$download_rate = 20.5;
if(file_exists($local_file) && is_file($local_file))
{
header('Cache-control: private');
header('Content-Type: application/octet-stream');
header('Content-Length: '.filesize($local_file));
header('Content-Disposition: filename='.$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);}
else {
die('Error: The file '.$local_file.' does not exist!');
}
?>
Downloading Files with PHP Normally, you don't necessarily need to use any server side scripting language like PHP to download images, zip files, pdf documents, exe files, etc. This application works mainly on the header of the PHP. This type of file download script I have used in most of my web application. When you want to download any file you need to send the file name to this application, rest of the thing PHP will handle. Find the example file and demo below.
I'm making a simple download script, where my users can download their own images etc.
But I'm having some weird problem.
She challenges Saraswatichandra to rescue Kabir. They hide the same from Danny. They inform Vidyachatur, Dugba and Guniyal that his brother, Kabir's life is in danger. Saraswatichandra and Kumud return to Vidyachatur's house. Saraswatichandra episodes 228.
When I've downloaded the file, it's having the contents from my index.php file no matter what filetype I've downloaded. My code is like so:
$r is the result from my database, where I've stored size, type, path etc. when the file is uploaded.
UPDATE
When I'm uploading and downloading *.pdf files it's working with success. But when I'm trying to download *.zip and text/rtf, text/plain it's acting weird.
By weird I mean: It downloads the full index.php file, with the downloaded file contents inside of it.
ANSWER
I copied this from http://php.net/manual/en/function.readfile.php and it's working now. It seems that : ob_clean(); did the trick! Thanks for the help everyone.
2 Answers
Try this function , or implement these headers to your code
SagarPPanchalI copied this from http://php.net/manual/en/function.readfile.php and it works now. ob_clean(); did the trick. Acer travelmate 5720 drivers.