Why is this not working?
if(!preg_match('#^(gif|jpg|jpe?g|png)$#i',$type)){
unlink($HTTP_POST_FILES['userfile']['tmp_name']);
die('file was wrong image-type');
}
I kept getting the file was wrong image-type. I tried to upload a .jpeg file.
By the way, can anyone point me to an explanation on the use of this , $ or (?<=x).
Here is the full code.
<?php
$uploaddir = '/www/home/alumni/images/gallery/';
$uploadfile = $uploaddir . $HTTP_POST_FILES['userfile']['name'];
$fileName = $HTTP_POST_FILES['userfile']['name'];
if(!is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'])){
die('Hacking Attempt!');
}
list($width, $height, $type, $size) = getimagesize($HTTP_POST_FILES['userfile']['tmp_name']);
echo "Size: ".$size."<br>";
if($size == false){
unlink($HTTP_POST_FILES['userfile']['tmp_name']);
die('file wasn\'t an image');
}
echo "Explode: ".explode('/',$size['type']);
list(,$type) = explode('/',$size['type']);
if(!preg_match('#^(gif|jpg|jpe?g|png)$#i',$type)){
unlink($HTTP_POST_FILES['userfile']['tmp_name']);
die('file was wrong image-type');
}
if (move_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'], $uploadfile)) {
print "File is valid, and was successfully uploaded.<br>";
list($width, $height, $type, $attr) = getimagesize("/www/home/alumni/images/gallery/".$fileName);
print "Here's some more debugging info:\n";
print_r($HTTP_POST_FILES);
} else {
print "Possible file upload attack! Here's some debugging info:\n";
print_r($HTTP_POST_FILES);
}
print "</pre>";
?>
If I delete this part:
if(!preg_match('#^(gif|jpg|jpe?g|png)$#i',$type)){
unlink($HTTP_POST_FILES['userfile']['tmp_name']);
die('file was wrong image-type');
}
then it works.
ljCharlie