no faking it - File type
Moderator: General Moderators
-
malcolmboston
- DevNet Resident
- Posts: 1826
- Joined: Tue Nov 18, 2003 1:09 pm
- Location: Middlesbrough, UK
no faking it - File type
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
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
- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
-
malcolmboston
- DevNet Resident
- Posts: 1826
- Joined: Tue Nov 18, 2003 1:09 pm
- Location: Middlesbrough, UK
- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
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.
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
...
as feyd said, getimagesize($image) would be very helpful as it returns the extension of the file in index 2
Do this:
Hope this helps.
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 }Or this
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; }