Simple File Server and Downloader Script

Nov 2, 2008 | Tags: HTTP | del.icio.us del.icio.us | digg Digg

If your blogs or sites have download links to your files, you might want to put all of your files in one directory rather than have them scattered throughout your site. This way you can manage them easier.

To download these files, you provide a downloader script. The script can be used for protection from direct download, check user's authentication, write logs, and such.

I've made a simple script for this purpose. It has features:

  • Can download any files: zip, images, pdf, php, etc.
  • Displays a 'You are downloading...' page before sending the file.
  • Simple and easy to develop.

Related Articles

The Downloads

28 Comments

Dennis on May 11, 2008:

Hi Nashruddin,

In download.php file, i realise that download.php must be place in the same directory of the folder.Is it possible that i can place it public_html/download.php ,while my download folder is at public_html/upload/? dirname(__FILE__);

Nash on Jun 5, 2008:

Hello Dennis,
Yes it is possible to place download.php in some other directory. Suppose your files reside in /var/www/pub/downloads, just change the line:

$fileserver_path = dirname(__FILE__);

with this:

$fileserver_path = "/var/www/pub/downloads";

petter on May 21, 2008:

Hi!
Is it possible to have the files on a remote server, rather on the local server, if not do you know of a script that can do this.
thx
Petter

Nash on May 22, 2008:

This script and your files should be in the same domain. otherwise the script wont work. Sorry I dont have info for such a script.

Adam on Jul 26, 2008:

mas kalo script model situs spt ini http://www.whereweget.com/ itu dimana yah dapetinnya?
mohon info.
thx before

Nash on Jul 28, 2008:

sori aku ga tau downloadnya dimana. tapi aku juga rencana bikin skrip kayak gitu kok. tungguin ya.

jared on Jul 30, 2008:

everything is working great, however the direct link to the file is not putting the directory in which the file resides.
so the absolute path is domain.com/assets/file.zip
however the link just links to domain.com/file.zip

how do i correct this?

Nash on Jul 31, 2008:

You mean:
1. you put the downloader script at http://www.yourdomain.com/download.php
2. your files reside in http://www.yourdomain.com/assets

if that is the case, change the variable $fileserver_path to the absolute path where your files reside. ex:

$fileserver_path = '/var/www/assets';

jared on Jul 31, 2008:

Did that, the auto refresh works just fine, its the html link that is not working. The html link is showing http://dl.domain.com/filename.zip which throws me a file not found error but it should read http://dl.domain.com/assets/filename.zip.

Nash on Aug 1, 2008:

Oh I see. It seems like there's a bug in my script. Modify the following line:

"...If it doesn't start, follow this <a href="<?=$req_file?>">link</a>"

to this:

"...If it doesn't start, follow this <a href="assets/<?=$req_file?>">link</a>"

This should solve your problem.

Jared on Aug 11, 2008:

sweet. thx, that did it.

drizad on Sep 13, 2008:

Nice script you have there. Can you add a feature to to monitor how much download that is made to the file? It's good to know how many download is made, e.g. for each month.

Thank you again.

Nash on Sep 15, 2008:

That's a good idea. I'll put it on my todo list.

manbros on Sep 17, 2008:

This is good and very simple script. Looking for audio / video download script, anyone having may please guide.

Mike on Dec 14, 2008:

Any good script deserves a good readme file. Can you add one to the installer if you have time? This looks promising.

Ben on Dec 20, 2008:

I am not sure what the download link needs to be to the script. As there was no readme file in the download. Any help would be greatly appreciated. Downloaded the script today

Nash on Dec 21, 2008:

Sorry the README.txt was missing I've added a new one.

Sobat Lama on Dec 27, 2008:

still i can use (direct) download manager to downlod these files :d

Nash on Dec 28, 2008:

That's true because I don't protect my files for direct download. to protect your files, use htaccess.

Ciberyan on Jan 18, 2009:

Hi Just discover your script Seems simple but I would like to add an authentication Any idea to achive that (I ma not a specialist ...) Ideally, user would hav to log (one time !) to gain access to download duriong a session or so Thanks in advance for your help

Nash on Jan 19, 2009:

If you already have a login page, you could add the code below to make sure only registered users can access the download page.

<?php
session_start();

if (!isset($_SESSION['login']['user_id'])) {
    die('Restricted access!');
}

/* ...the rest of code here... */
?>
replace $_SESSION['login']['user_id'] with your session vars.

Ali on Jan 26, 2009:

Is it possible to have download.php file in main folder and to download files from sub-folders? thx

Nash on Jan 26, 2009:

Yes. Just change the variable $fileserver_path to the directory where your files reside.

Max on Feb 26, 2009:

Is there a way to limit the amount of files downloaded in a 24 hour, I would like to limit my users to only 2 files per day or how to stop leaching.

Nan on May 1, 2009:

This sounds like something I have been looking for. Is it possible for you to add a redirect page to someone who is trying to link directly to the file -- possibly a redirect to a page that I can set up to let ppl know why we have switched to this type of download script?

Thanks!

Nash on May 7, 2009:

Yes you can do it. Only not with this script. Use htaccess instead to block your files and display messages or such.

steve on Aug 2, 2009:

Does this work with windows pathing?
I'm having troubles...

xenogen on Apr 23, 2010:

Hi! Thanks for your script. I followed your 3 steps and when I click the link it says "file no exist". What is the reason?

I changed the file server path only in download.php.

I use xampp and my domain name is localhost.

thank you very much.

Leave a comment

Name (required)
Email (will not be published) (required)
Website

Characters left = 1000

Tags

Recent Posts

  1. OpenCV Utility: Reading Image Pixels Value
  2. OpenCV Circular ROI
  3. OpenCV 2.0 Installation on Windows XP and Visual Studio 2008
  4. Runtime ROI Selection using Mouse
  5. Real Time Eye Tracking and Blink Detection
View Archives

About the Author

avatar Cool PHP programmer writing cool PHP scripts. Feel free to contact
Tel. +62 31 8662872
+62 856 338 6017
ICQ 489571630
Skype dede_bl4ckheart
Yahoo dede_bl4ckheart
Google nashruddin.amin

Recommended Sites:

Hacker's HTTP Client
HTML and CSS Tutorials
Stop Dreaming Start Action
Online Quran and Translation