Jump to content

Uploading photos securely?


Rommeo

Recommended Posts

I have users becoming members and allowed them to upload their own photos. But when they try to upload 5MB photos, it takes time to upload the photo, and sometimes server gives a timeout error.

 

I have searched and found javascripts that uploads to the server but I have noticed that it has security problems.

 

So how do you let users to upload photos ?

Link to comment
Share on other sites

You should use AJAX for uploading, if you have relatively big files to transfer. That way you can show a nice progressbar or just "Uploading..." status for the client untill it's finished. For that you have to create a JavaScript to send the HTTP request and data, that's not a problem. But before you load the page you allow your users to upload, you should create some kind of hash in PHP and attach it to the site JavaScript. Then when a user makes his request to upload file, you have that hash stored in an session and you compare it to the one the user sent. Now you can start the transfer.

 

Before starting the transfer, you should set_time_limit(int time_in_seconds) to allow the script be active for example 2 minutes and after that show an error message.

 

There might also be some concern about the REAL file type being uploaded. You cannot just blindly trust the MIME type of the header or the file extension. For that I don't have a specific solution. You could try to find some class that could verify a proper photo format reading the file itself, but I think that is pretty far fetched. I think you could for example try to install ImageMagick http://www.imagemagick.org/script/identify.php on the server. And upon upload, check the temp file with the program and according to results -> do actions.

 

When that is done, you are safe to save your image and return the response. Which you really don't have to do since when PHP finishes the respond is sent. And in JavaScript you just for response ready state for 200. If that appears, the transfer was completed and now you can let the user do some more stuff.

Link to comment
Share on other sites

I'm not exactly sure what you're asking as the title suggests how to secure an image upload but your actual post suggests you want to avoid the timeout error.

 

An AJAX upload is no more secure than a traditional form upload. AJAX will only improve the user experience. If you're worried about eavesdropping then the best solution is to aquire an SSL certificate and use HTTPS.

Link to comment
Share on other sites

Like the182guy said using ssl is the most secure option.. anyhow, on most sites checking the file type is sufficient solution..

 

and what comes to the timeout problem: you should pump up the max_input_time and max_execution_time php.ini confs... max_input defaults to 60 and max_exec defaults to 30... if people have slow connections it just isnt enough time to upload a 5mb file...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.