---
title: PHP - Stop Malicious Image Uploads
author: Brett Langdon
date: 2012-02-01
template: article.jade
---
Quick and easy trick for detecting and stopping malicious image uploads to PHP.
---
Recently I have been practicing for the upcoming NECCDC competition and have
come across a few issues that will need to be overcome, including how to stop
malicious image uploads.
I was reading
this
article on
Acunetix.com
about the threats of having upload forms in PHP.
The general idea behind this exploit for Apache and PHP is when a user can
upload an image whose content contains PHP code and the extension includes
‘php’ for example an image ‘new-house.php.jpg’ that contains:
```
... (image contents)
... (image contents)
```
When uploaded and then viewed Apache, if improperly setup, will process the
image as PHP, because of the ‘.php’ in the extension and then when accessed
will execute malicious code on your server.
## My Solution
I was trying to find a good way to remove this issue quickly without opening
more security holes. I have seen some solutions that use the function
getimagesize
to try and determine if the file is an image, but if the malicious code is
injected into the middle of an actual image this function will still return
the actual image size and the file will validate as an image. The solution I
came up with is to explicitly convert each uploaded image to a jpeg using
imagecreatefromjpeg
and
imagejpeg
functions.
```php