PHP Download Script
This is a PHP script that can be used to manage the download process. The advantages of using PHP script for handling the download process is we can prevent user to download the file directly, we can also limit the download speed to the desired speed, and we can also count how many times that file has been downloaded.
The script below has some features such as :
- Configurable download speed.
- Resumable download capability.
<?php
// If user click the download link
if(isset($_GET['filename'])){
// The directory of downloadable files
// This directory should be unaccessible from web
$file_dir="/tmp/";
// Replace the slash and backslash character with empty string
// The slash and backslash character can be dangerous
$file_name=str_replace("/", "", $_GET['filename']);
$file_name=str_replace("\\", "", $file_name);
// If the requested file is exist
if(file_exists($file_dir.$file_name)){
// Get the file size
$file_size=filesize($file_dir.$file_name);
// Open the file
$fh=fopen($file_dir.$file_name, "r");
// Download speed in KB/s
$speed=5;
// Initialize the range of bytes to be transferred
$start=0;
$end=$file_size-1;
// Check HTTP_RANGE variable
if(isset($_SERVER['HTTP_RANGE']) &&
preg_match('/^bytes=(\d+)-(\d*)/', $_SERVER['HTTP_RANGE'], $arr)){
// Starting byte
$start=$arr[1];
if($arr[2]){
// Ending byte
$end=$arr[2];
}
}
// Check if starting and ending byte is valid
if($start>$end || $start>=$file_size){
header("HTTP/1.1 416 Requested Range Not Satisfiable");
header("Content-Length: 0");
}
else{
// For the first time download
if($start==0 && $end==$file_size){
// Send HTTP OK header
header("HTTP/1.1 200 OK");
}
else{
// For resume download
// Send Partial Content header
header("HTTP/1.1 206 Partial Content");
// Send Content-Range header
header("Content-Range: bytes ".$start."-".$end."/".$file_size);
}
// Bytes left
$left=$end-$start+1;
// Send the other headers
header("Content-Type: application/octet-stream");
header("Accept-Ranges: bytes");
// Content length should be the bytes left
header("Content-Length: ".$left);
header("Content-Disposition: attachment; filename=".$file_name);
// Read file from the given starting bytes
fseek($fh, $start);
// Loop while there are bytes left
while($left>0){
// Bytes to be transferred
// according to the defined speed
$bytes=$speed*1024;
// Read file per size
echo fread($fh, $bytes);
// Flush the content to client
flush();
// Substract bytes left with the tranferred bytes
$left-=$bytes;
// Delay for 1 second
sleep(1);
}
}
fclose($fh);
}
else{
// If the requested file is not exist
// Display error message
echo "File not found!";
}
exit();
}
?>
<html>
<head>
<title>Home</title>
</head>
<body>
<a href="index.php?filename=file.pdf">Download</a>
</body>
</html>
?>
Tags:
Comments
Anonymous (not verified)
Sun, 08/14/2011 - 23:25
Permalink
How to
How to use this? Give a tutorial pls
admin
Thu, 08/18/2011 - 15:37
Permalink
It's easy to use it. You just
It's easy to use it. You just need to put your files into a directory. The directory can be changed from the $file_dir variable. And then in your HTML you just need to create link that point to this script and pass the filename as your query string, eg. yourscript.php?filename=yourfilename.pdf.
Anonymous (not verified)
Mon, 02/27/2012 - 21:05
Permalink
Really nice script, and it
Really nice script, and it seems like it will be very easy to integrate with a database of files. Thanks
Mohammad (not verified)
Wed, 10/24/2012 - 03:11
Permalink
put file in other host
i user this script in my main site (example www.main.com) , but my files uploaded in host dl (example www.dl.main.com) . when i put my files from dl.main.com into directory . it gives an error . please tell me how can i fix this problem . thanks a lot
Justin (not verified)
Fri, 06/27/2014 - 01:34
Permalink
file not found
I got it to work on my localhost wamp server, but I cant get it to work on my webserver using Cpanel. whatever I do it can't find the file: file not found. The path is right, I can pull up the file in the search bar. Any suggestions?
Justin (not verified)
Fri, 06/27/2014 - 03:15
Permalink
file not found
I added this echo snippet:
// Replace the slash and backslash character with empty string
// The slash and backslash character can be dangerous
$file_name=str_replace("/", "", $_GET['filename']);
$file_name=str_replace("\\", "", $file_name);
echo ($file_dir.$file_name);
And when I run the file, I get the path and file, copy and paste it into the browser, and it opens up the file. So I know I have the path right. Could it be a permission problem?
Justin (not verified)
Fri, 06/27/2014 - 04:11
Permalink
Success
Ok I figured it out. The path was wrong, I need to learn about paths! I was trying to use:
www.mysite.com/file_folder (which works if I type it in the browser manually)
The correct path was:
/home/******/public_html/file_folder/
As a sidenote, adding the echo snippet caused another problem, "Warning: Cannot modify header information - headers already sent by...". I deleted the echo and it worked great.
Thanks for the script!!
paventhan (not verified)
Fri, 04/05/2013 - 20:05
Permalink
hai buddy
its too good..
thank u very much..
Anonymous (not verified)
Sun, 01/15/2012 - 03:40
Permalink
Google
Thanks for this handy script! I'm using it for images: http://bossanova.im/en/photography/wallpapers/index.html
Will this script prevent Google from indexing my images? I actually want Google to index them, so that they show up in Google Image searches.
amin (not verified)
Tue, 03/20/2012 - 05:58
Permalink
Nice Script
Great script;
Thank you so much.
Bernd (not verified)
Thu, 05/17/2012 - 15:25
Permalink
Download Speed
In which way i must edit your script to remove the download speed limit ? I got an other script running which handles that, so i don't need a second one.
Thanks !
Anonymous (not verified)
Wed, 05/23/2012 - 02:35
Permalink
You have an error
if($start==0 && $end==$file_size){
should be
if( !$start && $end==$file_size - 1){
Sam F P (not verified)
Tue, 08/21/2012 - 17:42
Permalink
Good Job
Thank you for this neat scrip.
Sajid (not verified)
Sun, 09/09/2012 - 13:51
Permalink
Wordpress
How do i can implement this on WordPress site?
Shahzada (not verified)
Thu, 11/22/2012 - 21:52
Permalink
Thankuuuuuuuuuuu
Bundles of Thanxxxxxxxxxxxxxxxxxxxxxxxxxxx okaaaaaaaaaa g
VentyCZ (not verified)
Wed, 12/26/2012 - 23:57
Permalink
Error
Good script, but when I try to download file...there is a error:
Cannot modify header information - headers already sent ...
How to repair that - I already repaired File not found error by removing first / in file_dir ...
Thx VentyCZ
VentyCZ (not verified)
Wed, 12/26/2012 - 23:57
Permalink
Error
Good script, but when I try to download file...there is a error:
Cannot modify header information - headers already sent ...
How to repair that - I already repaired File not found error by removing first / in file_dir ...
Thx VentyCZ
dav (not verified)
Wed, 01/09/2013 - 06:50
Permalink
nice stuff
this script is good. good work
GJHGJHGJHG (not verified)
Tue, 03/12/2013 - 02:08
Permalink
GHFHGFHFHFH
GJHGJHJKHJKHKJH
Erik (not verified)
Sat, 04/20/2013 - 04:36
Permalink
Not working in opera
I was looking such a script. I''v found this one, works great in FF IE and Chrome but not in Opera.
Opera doesn''t start the download, but the script works fine in the other browsers. Great work thanks.
Version 12.15
Buils 1748
Platform Win32
Systeem Windows 7
Browseridentificatie Opera/9.80 (Windows NT 6.1) Presto/2.12.388 Version/12.15
Alexandru (not verified)
Sat, 11/16/2013 - 04:24
Permalink
Thank you.
Thank you for this script. Simple and useful.
vhieucom (not verified)
Sun, 06/29/2014 - 10:20
Permalink
not show info downloaded
Hi, thanks you code,
Kamlesh (not verified)
Fri, 07/04/2014 - 21:43
Permalink
Can I download file from other server?
I think this code only work if file on our server but if we want to download file from other server or site then this code not work.
Neeraj (not verified)
Fri, 07/04/2014 - 21:48
Permalink
Download start from 0 bytes when I pause everytime
I have a problem in this script, when I pause to download (IDM) and then start it start download from zero byte even I downloaded some bytes before pause.
What is the problem? can you fix this??
Sharon (not verified)
Tue, 08/26/2014 - 19:49
Permalink
Support and Help
This PHP download script has many features like the increased configurable speed and resumable download capability is also very high in this. The click information of the article proved very much helpful for me as this is very easy to use.
Add new comment