views:

88

answers:

3

Hi,

i have a script that allows only authorised users to upload files to a certain folder.

however i do not know how to prevent people from downloading freely without login.

I need the solution in php.

I have googled around but nothing straight forward as yet.

currently in my document root i have a folder called admin and a subfolder called uploads inside the admin. so only admin role can upload . both editor and admin can download. what should i do in this case?

Please advise.

+4  A: 

Put the files somewhere outside the public webroot directory, or configure your server to not serve the files. As long as your server will happily serve everything with a valid URL, there's nothing you can do with PHP to prevent that.


If your files are in the /public_html/ folder, take them out of that folder and place them in e.g. /secret_files/, so your directory structure looks something like this:

public_html/
    index.html
    admin/
        admin_index.php
secret_files/
    my_secret_file.txt

The webserver is only configured to serve files in the /public_html/ directory, so nobody will have access to directories outside (technical term above) it.

To still enable somebody to download those files, do as cletus suggests and use readfile to "manually serve" the files via a PHP script. PHP will still have access to these other parts of the file system, so you can use it as a gatekeeper.

deceze
is the public webroot the public_html? wat about httpdocs?
keisimone
@keisimone See updated answer.
deceze
@deceze thanks. that is much clearer. you guys are cool.
keisimone
+4  A: 

Answering question on how to password protect with PHP: This should solve your problem.

twodayslate
and what about someone find the password ?
RageZ
If they find the password, most likely they have access to your FTP so you are screwed anyways.
twodayslate
+5  A: 

Don't store the files in a directory under the document root.

Instead move them somewhere else and then a PHP script can programmatically determine if someone can download them and then use readfile() or something similar to stream them to the user.

You could also configure the Web server to not serve files from this directory but then you need PHP to serve them anyway. It's cleaner simply not to put them under the document root.

cletus
hi cletus, i am not strong in programming so I need to ask you for more specifics1) where is somewhere else? currently in my document root i have a folder called admin and a subfolder called uploads inside the admin. so only admin role can upload . both editor and admin can download. what should i do in this case?
keisimone
@keisimone (1) Store them above the document root, which *may* be somethign like htdocs or public_html.
alex
@alex yes all my files are already in public_html :) so its public_html > admin > uploads
keisimone
@keisimone can you move them **above** your docroot. So accessing them from your public_html would like like this '../downloads/file.txt'
alex
@alex so you are saying that the downloads folder is on the same level as the public_html?
keisimone
@keisimone Yes, or higher. You should not be able to go to www.yoursite.com/downloads by default.
alex
@cletus Though you had the right answer earlier than anyone else, i have to say that deceze was the first one that made me GET IT. Of course credit to alex here as well. So i will mark deceze as correct answer instead of yours. I apologise if you are offended in anyway.
keisimone
@keisimone no need to apologize. deceze's answer is good. i only take issue if the accepted answer is unclear, misleading, incomplete or wrong (when there's a better alternative)
cletus
@cletus Thank you :) Oh the thing worked brilliantly. I am very pleased. A while ago, when i asked a sql related query i got a high quantity of answers but the quality was low. This time it is the opposite, i am not sure what i did went right this time. Hopefully the next time i post in SO, i get the same level of satisfaction as i did this time.
keisimone