Serving files through PHP

Posted by guru in September 12th, 2007
Published in PHP
UPDATE:
- I’d like to thank a friend who detected this bug:
Microsoft Windows users, please change the following line:

if (realpath($allowedfilepath).”/$file” …
to,
if (realpath($allowedfilepath).”\\$file” …
 

Serving files through PHP is basically needed in two cases:
1) When you want to count the number of times it has been downloaded.
2) When you don’t want to give direct access to the file from your server. (Hiding or protecting file path)

-Being able to serve files through PHP can also be useful when you want only specific users to download the files in your server, for example: Only registered users, or users who pays for the file, like online PDF books.
-You can also use it to ban or allow specific files to your users.
It gives you full control over transferring of files in your server.

Here is a code snippet to serve a file using php script.

Step1: Copy the following code and save it as, getfile.php:

To use the code:
1) Make a subfolder inside inside your home directory named ‘files’.
2) Put all your files that you want to server through php, inside that folder.
3) Copy and save the program as ‘getfile.php’ on your home directory.
4) Restrict direct access to all the files inside the sub-folder (files) you just created.

Restricting direct access your files:
If you do not restrict direct access to the files you wan to server through your PHP program, people can directly access/download the file like:
http://your-site.com/files/book1.pdf

Step2: To prevent this, make a file named ‘.htaccess’ inside the ‘files’ directory and write following text on that file:
#deny all access
deny from all
IndexIgnore *


Now, your files won’t be accessible directly like mentioned above, users will only be able to access the file like:
http://your-site.com/getfile.php?file=book1.pdf

That is, if you have allowed this on getfile.php.

You can also make a database to store number of times a particular file has been downloaded.
If you want a code to do this, please comment below and let me know.

!Cheers

21 comments so far.

Follow-up this post comment rss or leave a trackback
mygif
Ros wrote,

Thanks guru,
Wow it rocks man, I was worried how to hide real path of my ebooks that i sell.

It just rocks!

mygif
Rahul wrote,

Please write for ASP too >:-) I will thank you 100 times.

mygif
Mhd wrote,

hey it give all error. where put this getfile.php? I put it cgi but all error. I in UAE so need this very much please help!!!!!!

I thanks you lot.

mygif
Dan wrote,

Hey guru,

I’m trying to build a software library for my school project, I need to be able to count the number of times a software has been downloaded.

I’ll be using your code as a reference.

Thanks

mygif
Austin wrote,

Does geocities support php? I need to use this in there, any help??

CIAO

mygif
BigBen wrote,

Does geocities support php?? lol, which part of world are you in????

mygif
guru wrote,

Thanks for your comments guys:
Hey Rahul, I’ll try to do that as soon as I have time.

Mhd, you gotta put ‘getfile.php’ on your home directory, so it can be accessed like:
http://yoursite.com/getfile.php?file=myfile.zip
and make a subfolder on your home directory named ‘files’ to store your protected files.

Dan, I’m just glad that my code will help you.

Austin, there are a lots of freehosting sites supporting php and mysql:
See: http://technociti.com/forum/func,view/id,9/catid,18/

mygif
kakar wrote,

My files are located in

www.site.com/user/sites/demo/FILES ARE HERE

How can i do the same thing?

mygif
BigBen wrote,

Kakar,

It’s easy at line 3 you can see that you are given place to write real path there, so you try by writing: user/sites/demo/FILES/

mygif
guru wrote,

Hi kakar,
Put getfile.php on the main directory of the site,
so that, it can be accessed like: www.site.com/getfile.php,

now, open getfile.php with a text editor,
on line 3:
Change, $allowedfilepath=”files/”; to
$allowedfilepath=”user/sites/demo/FILES/”;

Thats it!
You can now download files inside “user/sites/demo/FILES/” like www.site.com/getfile.php?file=file1.zip

And, don’t forget to put the .htaccess file inside: user/sites/demo/FILES/

Enjoy!

mygif
tomasbacon wrote,

Hey guys, any luck on getting the script in place to count the number of downloads?

mygif
tomasbacon wrote,

hey guys, any chance of getting the code for counting the number of times the file is downloaded?
Thanks

mygif
guru wrote,

Hi tomasbacon,
You can make a table on your database, with filename and download count fields, and add 1 to the counting field, eachtime the getfile.php serves that file. If you want me to write a code for that, tell me n’ I do it asap

mygif
boka wrote,

hey tomasbacon, it’s very easy to make a database for files. its just few line code. If I got time till tomorrow morning I will post code for you here in comment section.

mygif
jack wrote,

I’m looking for same code, I’m waiting boka.

thanX

mygif
Topsecrets wrote,

Hey Guru I’am still having trouble with this scipt can u pls help?.

I done exatly everything you have said step by step and am still not getting results.

is there a step missing that i should know about?..

Thanks in advance..

mygif
guru wrote,

Hey topsecrets,
Please tell us exactly what kind of problem you are facing,

If you can get it working in any case,
You can contact me directly at online[at]nishadh.com.np or discuss your problem on technociti forums, http://technociti.com

Hope you get it working soon, good luck!

mygif
Marin87 wrote,

What if i have lots of subfolders inside folder files/ For exalmple: files/ts1 ; files/ts2
I have only one getfile.php in my root, how to add 2, 3, 1000 $allowedfilepath-S??

Any idea?

mygif
guru wrote,

You can always modify the code to add such or any other functionalities. You are free to modify your code as needed. If you want to allow only specific folders, make an array of those folders like ‘files/f1/f2′, and check each of them for authorization before letting files inside it to be downloaded. You can also choose to allow every files and folders within the main allowed folder without using an array, I’ll add a code for it once I get time.

Keep the ideas coming…

mygif
TopsecreTS wrote,

well done guru, we worked on a little bug we found in the coding and we managed to fix it all. refer to guru for more details.

mygif
Rob wrote,

Thanks for the fix, guru, it’s working for me too now.

Leave A Reply Below

for "Serving files through PHP"

 Username (*required)

 Email Address (*hidden)

 Website (*optional)

 Website (*optional)

Social Feeds

Topics Search

Main Translator

French

German version

Spanish version

Italian version

Categories

Recent Articles

Recent Posts

Recent Commentes

August 2008
M T W T F S S
« Oct    
 123
45678910
11121314151617
18192021222324
25262728293031

Blogroll

Navigation

Share!

Add to Technorati Favorites
Add to Google Add to Windows Live