Here is the code in upload_processor.php
:
include_once 'functions.php';
$name = $_FILES['upload-image']['name'];
$type = $_FILES['upload-image']['type'];
$size = $_FILES['upload-image']['size'];
$temp = $_FILES['upload-image']['tmp_name'];
$error = $_FILES['upload-image']['error'];
img_processor($temp, $error, $size)
And here is functions.php
:
function img_processor($img_temp, $img_error, $img_size){
if($img_error===0){
if($img_size < 4194304){
if( $proc_img = @imagecreatefromjpeg($img_temp) ){
imagejpeg($proc_img,'../uploaded/something.jpeg');
} elseif( $proc_img = @imagecreatefrompng($img_temp) ){
imagepng($proc_img,'../uploaded/something.png');
} elseif( $proc_img = @imagecreatefromgif($img_temp) ){
imagegif($proc_img,'../uploaded/something.gif');
} else {
echo("Only JPEGs, PNGs, and GIFs are allowed");
}
if(isset($proc_img)){
echo("upload complete");
}
} else {
echo("Your file was too big. Only images that are 4MB or less are allowed");
}
} else {
echo('Error uploading file! Code '.$img_error);
}
}
The basic idea is to recreate the image, then rename it so that no one can upload something like malicious_code.php.jpg
.
What are the holes in this code? Are there better ways to protect my site from PHP-injected images?