Page 1 of 1

Need Some help with file functions

Posted: Mon Feb 05, 2007 10:33 pm
by blizzard_jdf
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have a function that takes in a file. What I want to is print "Success" if the extension is correct and print "fail" if it is not an acceptable extension. I have written two functions. The checkImageExtension works fine. Not sure if it is missing something. However checkImageType($file) does not work at present. It is supposed to take an uploaded file and get the filename as a string. I have been testing using a local file. Can anyone help me?

Code: Select all

function checkImageExtension($name) {
// List of all acceptable extensions
$array_of_extensions = array("jpg" , "jpeg" , "bmp" , "gif" , "png");
$partitioned_image_src = explode("." , $aStr_image_src);
// Obtains the file's extension by getting the last partition of the exploded string
$image_extension = $partitioned_image_src[count($partitioned_image_src) - 1];
if (in_array($image_extension , $array_of_extensions)) {
// Renames the string to a form acceptable for linux
$aStr_image_src = str_replace(" " , "_" , $aStr_image_src);
$this->str_image_src = $aStr_image_src;
return true;
}
else {
return false;
}

function checkImageType($file) {
$filename = basename($file);
if (checkImageExtension($filename)) {
return "Success";
}
else {
return "Failed";
}

}

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Feb 05, 2007 11:05 pm
by feyd
Just so you know, the extension of a file has little to nothing to do with what the file is internally.

For example, renaming a jpeg image .gif doesn't change that it's still a jpeg.

getimagesize() and mime_content_type() may be of interest.

Posted: Mon Feb 05, 2007 11:12 pm
by superdezign
You can easily get the type from $_FILES['whatever']['type'], but it's nice to double check using regex to get whatever comes after the dot.

Posted: Tue Feb 06, 2007 4:38 am
by Ollie Saunders
You can easily get the type from $_FILES['whatever']['type'], but it's nice to double check using regex to get whatever comes after the dot.
That's only the type that the browser says it is, the browser won't always get it right and it can be spoofed. getimagesize(), as feyd suggested, and exif_imagetype() are your best bets. There's some stuff in this thread besides the guy going a bit mental and having his thread locked. ha ha funny stuff.

Posted: Tue Feb 06, 2007 7:00 am
by mikeq
There's some stuff in this thread besides the guy going a bit mental and having his thread locked. ha ha funny stuff.
That was indeed funny