Active5 years, 2 months ago
A gallery of PHP scripts for webmasters and programmers to download for free. Variety of script with examples that are ready for use in your web pages. Categorized collection of prebuilt PHP scripts with simple copy and paste codes. Hello friends this post is about Download Script to Download any File using PHP. So in this you will learn how to download a file in PHP but also you need to know this download actually force the browser to download file. But this is very easy simple few steps you just have to follow this code. In this tutorial, I'm going to show you how to write a PHP script that allows downloads. To allow downloads from a server, you need to write a script that can communicate with it effectively. PHP is a server-side scripting language and is well-designed for this task, with many versatile tools. For security reasons, web servers prevent PHP (or any server side scripting) files from being downloaded so that their source code is readable, unless there is a server failure while you attempt the download. Normally, you'll only get the HTML output of a PHP file on your local computer not the source code.
If you still get strange results when downloading (especially in IE), make sure that the PHP output compression is disabled, as well as any server compression (sometimes the server inadvertently applies compression on the output produced by the PHP script). Handling large file sizes. Readfile is a simple way to ouput files files.
Php File Download Script Example
I'm making a simple download script, where my users can download their own images etc.
But I'm having some weird problem.
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.
skolind
Php File Download Script Software
skolindskolind94466 gold badges2222 silver badges4444 bronze badges
2 Answers
Try this function , or implement these headers to your code
SagarPPanchal
6,73866 gold badges2929 silver badges5454 bronze badges
Erdinç ÇorbacıErdinç Çorbacı
I copied this from http://php.net/manual/en/function.readfile.php and it works now. ob_clean(); did the trick..
skolindFree Php Scripts
skolind94466 gold badges2222 silver badges4444 bronze badges
Not the answer you're looking for? Browse other questions tagged php or ask your own question.
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!');
}
?>