Need Some help with file functions

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
blizzard_jdf
Forum Newbie
Posts: 3
Joined: Mon Jul 31, 2006 8:02 pm

Need Some help with file functions

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

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post 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
Post Reply