PHP School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a PHP Expert!

Join 307,026 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,204 people online right now. Registration is fast and FREE... Join Now!




Uploading files: which is safer?

 

Uploading files: which is safer?

Zel2008

5 Nov, 2009 - 12:16 PM
Post #1

D.I.C Head
**

Joined: 6 Jan, 2009
Posts: 173


My Contributions
Hi everybody,
I have to do some file uploading, and I was curious, what is the best way to do that and make sure it's secure?

Thus far, I've found 2 different main ways:
1. Checking the file name (with regular expressions)
2. Checking the file content (on http://www.scanit.be/uploads/php-file-upload.pdf)

So...I'm pretty sure just checking the file name is nowhere near secure enough, after trial and error, and it seems like the second approach has holes too. So, I was wondering, what is the risk of uploading any file name given, then stringently checking its format against the allowed ones? Is it possible that a malicious user could use those bad files against me before I get a chance to check and/or delete them? All the files I'd have to check are text with very particular layout.

Can anyone give me some advice on this, please?

Thanks,
Zel2008

User is offlineProfile CardPM
+Quote Post


RudiVisser

RE: Uploading Files: Which Is Safer?

5 Nov, 2009 - 01:35 PM
Post #2

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,890



Thanked: 139 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

My Contributions
Just checking the filename will be fine, as long as you make sure that that file extension isn't getting executed by the webserver.

If people don't have to access the files once uploaded, simply place the files outside of the web root.
User is offlineProfile CardPM
+Quote Post

Zel2008

RE: Uploading Files: Which Is Safer?

5 Nov, 2009 - 01:48 PM
Post #3

D.I.C Head
**

Joined: 6 Jan, 2009
Posts: 173


My Contributions
Thanks RudiVisser,
So, you're saying that all I have to do is place the files outside of the web root, and that makes them completely safe so no one can access them or try to run them? There must be a way around that, isn't there?

Thanks,
Zel2008
User is offlineProfile CardPM
+Quote Post

RudiVisser

RE: Uploading Files: Which Is Safer?

5 Nov, 2009 - 01:53 PM
Post #4

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,890



Thanked: 139 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

My Contributions
As long as the PHP files you've written in the web root are secure and do not allow file inclusion via a variable - You'll be fine.

You could even change permissions so that the web server's user account can only write files, and (for example) an FTP account can read them. That way there's absolutely no chance, even if you did allow unsecured file inclusion, PHP could access the files.

This post has been edited by RudiVisser: 5 Nov, 2009 - 01:54 PM
User is offlineProfile CardPM
+Quote Post

Zel2008

RE: Uploading Files: Which Is Safer?

5 Nov, 2009 - 02:12 PM
Post #5

D.I.C Head
**

Joined: 6 Jan, 2009
Posts: 173


My Contributions
Thanks RudiVisser,
Hmm, that's interesting, but I don't think I get it. If I change the file permissions so the server can only write files, how does it read the files it uploads, and how can I use those files for anything? (I'm on a Linux Fedora system, if that helps.)

Thanks,
Zel2008
User is offlineProfile CardPM
+Quote Post

RudiVisser

RE: Uploading Files: Which Is Safer?

5 Nov, 2009 - 02:27 PM
Post #6

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,890



Thanked: 139 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

My Contributions
It depends exactly what you want to do with the files.. What is it that you want to do??

If your users are simply uploading files that you retrieve via an FTP account, then that will solve your issue.

If your users are meant to redownload the files, you can make sure that they are not in any way executed by creating a "pass-thru" script that will take the filename as a parameter and use readfile and sending the content back as application/octet-stream with Content-Disposition: attachment headers.
User is offlineProfile CardPM
+Quote Post

SeiferMaster

RE: Uploading Files: Which Is Safer?

5 Nov, 2009 - 02:28 PM
Post #7

New D.I.C Head
*

Joined: 28 Oct, 2009
Posts: 19



Thanked: 2 times
My Contributions
There shouldn't be a problem if you don't allow execution on the files you upload. Simply allow the users to read/write to the folder and it should be fine. Then again, if you simply want to upload images, you could always check the type of file that's being uploaded ($_FILES['type']).
User is offlineProfile CardPM
+Quote Post

Zel2008

RE: Uploading Files: Which Is Safer?

5 Nov, 2009 - 02:52 PM
Post #8

D.I.C Head
**

Joined: 6 Jan, 2009
Posts: 173


My Contributions
Thanks guys,
Just to be clear, what I'm doing is downloading a file from the user, and then that file is used as input to a text interface program. Once the program which uses the data in the file is run, the file is deleted. So, the file isn't executed in any way; it's just read into this program.

Does that make sense? Does that mean I could just have only the server have permission to read, write, and execute that folder? Because, if the user wants to download one of his or her results files, the server would just pipe it back, right?

Thanks,
Zel2008
User is offlineProfile CardPM
+Quote Post

RudiVisser

RE: Uploading Files: Which Is Safer?

5 Nov, 2009 - 02:53 PM
Post #9

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,890



Thanked: 139 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

My Contributions
Well if you're just taking the input, you won't even need to resave the file out of the temporary directory anyway, just read it directly (from $_FILES['formfield']['tmp_name']).

This post has been edited by RudiVisser: 5 Nov, 2009 - 02:53 PM
User is offlineProfile CardPM
+Quote Post

Zel2008

RE: Uploading Files: Which Is Safer?

10 Nov, 2009 - 10:28 AM
Post #10

D.I.C Head
**

Joined: 6 Jan, 2009
Posts: 173


My Contributions
Thanks RudiVisser,
I'm so sorry it took me a bit to reply; I didn't realize you put up an answer. That looks like the right way to go, so thanks again for your help.
Thanks,
Zel2008
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 08:19AM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month