Forum Moderators: coopster
I have done a bunch of things but let's what other members think
I was looking at something that uses the old lame extension test, which made me start looking through code to see what might be the best.
imagecreatefromjpeg will fail if the source is not a jpeg and, if it is a jpeg but has some other payload, the processing and output should remove any extraneous crap. (If it doesn't I'd be surprised.)
Further
using is_uploaded_file()
PHP upload a file in a temp so we can check it there:
<?
$filename= $_FILES['attachment']['tmp_name']';
if (is_uploaded_file($filename) )
{
\\$_FILES['attachment']['tmp_name']
}
?>
then by the same token use
move_uploaded_file() to move it in its final destination
it tells you filetype and mime and returns false on a non image
sample return I am looking at
Array
(
[0] => 308
[1] => 401
[2] => 2
[3] => width="308" height="401"
[bits] => 8
[channels] => 3
[mime] => image/jpeg
)
// Get the details of "imagefile"
$filename = $_FILES['imagefile']['name'];echo"$filename<br>";
// slashed not needed here code
else
{
$temporary_name = $_FILES['imagefile']['tmp_name'];echo"$temporary_name<br>";
$mimetype = $_FILES['imagefile']['type'];echo"$mimetype<br>";
$filesize = $_FILES['imagefile']['size'];echo"$filesize<br>";
$path_img="img/$filename";
$path_thumb="img/thumbs/t_$filename";
$title=Clean($_POST['title']);
$alt_tag=Clean($_POST['alt_tag']);
$description=Clean($_POST['description']);
// etc....
?>
$imgfile_name = 'something.jpg';
$uperror = '';
$maxwidth = 400; // max allowed width for image
$maxheight = 600; // max allowed height for image
$imginfo = getimagesize($imgfile_name);
if ($imginfo[2]!= 2 ¦¦!$imginfo) {
$uperror = "<p>don't be an idiot, that isn't an image or it isn't a jpeg both of which are necessary";
} else if ($imginfo[0] < 1 ¦¦ $imginfo[0] < 1) {
$uperror = "<p>your image to have a width or height of 0, that's not going to work";
} else if ($imginfo[0] > $maxwidth) {
$uperror = "<p>your image is too wide, the max allowed is " . $maxwidth;
} else if ($imginfo[1] > $maxheight) {
$uperror = "<p>your image is too high, the max allowed is " . $maxheight;
}
if ($uperror!= '') {
echo '<p>You screwed up and here is why:<br>',$uperror;
unlink($imgfile);
die();
}
you could always add more checks for other things you may require, I was just playing
NOTE: replace ¦ chars with real pipe characters
Apologies, I got a bit confused and jumped to the punchline. :) I actually do use getimagesize to determine the filetype (precisely because it rolls a couple different functions into one), then whatever type it returns dictates the appropriate imagecreatefrom* and image* function to filter through.
I didn't know about the exif functions [php.net], those look handy.