[S] File type checking

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

[S] File type checking

Post 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 !
Last edited by Calimero on Tue Jul 27, 2004 2:05 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
User avatar
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

...

Post by Calimero »

IF someone however submits evil file (virus, troyan etc...)
will this contaminate (in lack of better words, sorry) my computer
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

...

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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!');
}
User avatar
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

...

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

read the [php_man]getimagesize[/php_man]() page for the details of which values the type property holds.
User avatar
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

...

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

no.
User avatar
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

...

Post by Calimero »

So the php code is ok, as long as I dont touch SWITCH command right?

didnt quite understood you.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you didn't need to alter what you are sending to the switch.
Post Reply