views:

176

answers:

5

Hello, I'm experiencing my first form in php where images can be uploaded.

I've seen some article on the web which explains it can be dangerous, so there is some way to block scripts on a specified folder? Something with .htaccess or php .ini instruction?

+1  A: 

Your best bet is to verify the file's extension upon upload. If it's not jpg/png/gif/etc., dismiss it. As long as your webserver is not misconfigured to interpret any file as a PHP file, then with this approach you're out of harm's way, with minimal headache and really simple implementation.

Felix
A: 

i think it can be dangers if you don't check what file type was uploaded e.g. "hacker" uploads a php file that deletes all of your httpdocs stuff, or if people can upload to many or to big files.

antpaw
A: 

It's dangerous only if you let the users upload whatever they want. Allow only what you decide is safe and you won't need to block anything.

kemp
+1  A: 

The VERY best way is to make sure that your upload directory is outside of your webroot. As long as the webserver has read/write access there you will be fine - no worries about executable uploads. This was discussed here on stackoverflow.

Shane C. Mason
+1 for a good answer... The best way (well, as least as good as not uploading below the webroot) is probably to disable php completely for that directory. This sort of explains how (enough to go look in the docs for more details): http://www.faqts.com/knowledge_base/view.phtml/aid/128/fid/35
Fredrik
+1  A: 

Check the file being uploaded has a benign extension (.gif, .mp3, etc) - and trash anything else. For extra-sekrit protection, capture the file's original name in a database (for future reference), then encrypt the filename (and store that as well). That way anything that's uploaded can't be found by filename by the uploader.

b. e. hollenbeck
This wouldn't be that good of a way to do it. As I could just create what I want and save it as a .mp3 file... Your second suggestion is pretty clever though.
Joe
If it's uploaded to the server with an mp3 extension, the server isn't going to execute it when it's called again - and with the encryption, it really doesn't matter one way or the other.
b. e. hollenbeck