views:

556

answers:

2

I have a PHP script that processes file uploads. The script tries to organise the files that are uploaded and may create new folders to move the files into if needed. These files will be below the www root directory (ie, a web browser will be able to access them).

My question is, what permissions should I set for the folders that get created and for the files that are moved into them (using mkdir() and move_uploaded_file())?

+3  A: 

Your webserver needs read and write permission in those folders, execute permission should be revoked (assuming UNIX-like systems). If not, a user could upload a script and have it executed by sending a HTTP request for it.

But IMO the whole concept is a potential security hole. Better store the files in a folder outside the webserver root, so that no direct acceess is possible. In your web application, you can have a PHP download page that scans the upload directory and displays a list of download links. These download links lead to another script, that reads the fiels from you storage dir und sends them to the user.

Yes, this is more work. But the scenario is very common, so you should be able to find some source code with example implementations easily. And it it much less work that having your server hacked...

Treb
This is actually part of a CMS and the files are all images (which we verify - non-images are discarded). I understand the risk of someone, for example, uploading a php script
rikh
Ifg you can sanitise the input data, it sounds safe enough to me. But I still would unset the executable flag ;-)
Treb
The files themselves should definitely not have execute permissions set, but the directory they're put into will need execute permissions for the webserver user if you wish to provide listings of the directory's contents.
Dave Sherohman
A: 

to answer it specifically 766 (no execute permissions) would be the loosest you would want to use. On the other end 700 would allow no one but the web user to mess with the file. But really it all depends you were doing with the files that would determine the best result.

SeanDowney