Page 1 of 1

[S] File type checking

Posted: Sun Jul 18, 2004 3:29 pm
by Calimero
this is the problemo>

I use this syntax, not mine, so need help

field with name img1, method post
$_FILES['img1']['size']

can this be modified to check for file TYPE, need to check if the file is of JPG, JPEG or GIF

Thanks Ahead !

Posted: Sun Jul 18, 2004 3:32 pm
by feyd
I'd go with using [php_man]getimagesize[/php_man]() instead, as this will look at the binary data itself to determine the type..

...

Posted: Sun Jul 18, 2004 3:35 pm
by Calimero
IF someone however submits evil file (virus, troyan etc...)
will this contaminate (in lack of better words, sorry) my computer

Posted: Sun Jul 18, 2004 3:38 pm
by feyd
getimagesize() will just look at the binary data.. it doesn't run it.. if it doesn't recognize it, it fails.. try it out.

...

Posted: Sun Jul 18, 2004 3:45 pm
by Calimero
Ok I looked at the MANUAL.
Is this the right way of thinking

Code: Select all

<?php
<?php
if ($size = getimagesize($filename)) 
{//upload file}
else 
{//wont work}

?> 
?>
JUST interested will the line
if ($size = getimagesize($filename))
return true - reccognise thet this is an image, or do I need some extra code to do this

I just drafted the code, sorry for syntax errors.

Posted: Sun Jul 18, 2004 3:57 pm
by feyd
basically

Code: Select all

if(($size = getimagesize($filename)) !== false)
{
  switch($size[2])
  {
    case 1:
    case 2:
      // it's a GIF or JPEG file..
      break;
    default:
      break;
  }
}
else
{
  die('upload unacceptable!');
}

...

Posted: Sun Jul 18, 2004 4:00 pm
by Calimero
THANKS for that,
just to recap, these CASE 1 and CASE 2 - is there a list of filtypes in PHP (i presume) and you just called them from there - that little part I dont understand.

Posted: Sun Jul 18, 2004 4:02 pm
by feyd
read the [php_man]getimagesize[/php_man]() page for the details of which values the type property holds.

...

Posted: Sun Jul 18, 2004 4:06 pm
by Calimero
So to add PNG and BMP I do his correction:

Code: Select all

<?php
if(($size = getimagesize($filename)) !== false) 
{ 
  switch($size[2]) 
  { 
    case 1: 
    case 2: 
    case 3:
    case 6:
      // it's a GIF or JPEG file.. and now PNG or BMP
      break; 
    default: 
      break; 
  } 
} 
else 
{ 
  die('upload unacceptable!'); 
} 


?>
NOTE> I havent touched this line>

switch($size[2])

Do I need to change it to

switch($size[4])

so my version functions.

Posted: Sun Jul 18, 2004 4:07 pm
by feyd
no.

...

Posted: Sun Jul 18, 2004 4:09 pm
by Calimero
So the php code is ok, as long as I dont touch SWITCH command right?

didnt quite understood you.

Posted: Sun Jul 18, 2004 4:13 pm
by feyd
you didn't need to alter what you are sending to the switch.