no faking it - File type

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
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

no faking it - File type

Post by malcolmboston »

need to know a way of being certain of exactly what type of file PHP is dealing with.

Im building some PHP images functions and need to be sure of what typoe of file is being sent to the script (png, jpeg, EXE!!)

thanks
Mal
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

try exif_imagetype() if you are dealing only with images.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

basically its for an image thumbnail generator

it needs to work with all file types as the user can, in theory, attack the server by uploading an incorrect filetype.

i initially would like to scan for the extension and THEN scan for image type if i cant do it all in one.

Thanks
Mal
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

Just use the aforesaid function. Refer the PHP Manual for it. it basically tell you the type of image file it is. For generating thumbnails You can
use this info. first of all generate error if user uploads incorrect file. and if user tries to outsmart by changing its extension the function I mentioned will return the type of image only. if image-type returns unknown you can just reject the file and tell the user that it was an invalid file. :)
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

great, thanks

Mal
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

getimagesize() also can read and report back info on many differing image types. It doesn't require GD either :)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

...

Post by s.dot »

as feyd said, getimagesize($image) would be very helpful as it returns the extension of the file in index 2

Do this:

Code: Select all

// Get the imagesize indexes, including file type //
$type = getimagesize($image);

// All possible types returned by getimagesize(), edit to your liking //

$allowabletypes = array("GIF", "JPG", "PNG", "SWF", "PSD", "TIFF", "JPC", "JP2", "JPX", "JP2", "SWC", "IFF", "WMPB", "XBM");

// Check to see if the file type is in the allowed types array //

if(!in_array($typeї'2'], $allowabletypes)) { die("invalid file type"); } ELSE { // code here }
Hope this helps.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Or this

Post by s.dot »

Or you can alternatively do this, which will basically do the same thing

Code: Select all

// Get image type //

$imagtype = getimagesize($source);

// Check file type //

switch ( $imagetypeї2] ) { 

// Create cases for all file types you would like to allow //

case 2: $fileext = 'jpg'; break; 
case 3: $fileext = 'png'; break; 
default:

// File type not allowed, so report error //

echo ("Invalid File Type"); break; }
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you know you can edit your post right?
Post Reply