cgm225 Posted February 18, 2008 Share Posted February 18, 2008 I want to check a file name, and if it has the file extension jpg, jpeg, JPG, JPEG, gif, GIF, png, PNG (and all other variations), then I want the code to return TRUE. What would be the simplest way to do this? Thank you all in advance! Quote Link to comment https://forums.phpfreaks.com/topic/91780-checking-for-proper-file-extensions-if-present-return-true/ Share on other sites More sharing options...
jscix Posted February 18, 2008 Share Posted February 18, 2008 $x = substr($filename, -1, 4); if ($x == '.jpg') { // file is jpg } etc... generalization, but check up on it.. really easy to do. Quote Link to comment https://forums.phpfreaks.com/topic/91780-checking-for-proper-file-extensions-if-present-return-true/#findComment-470057 Share on other sites More sharing options...
redarrow Posted February 18, 2008 Share Posted February 18, 2008 <?php $file='redarrow.txt'; $file=substr($file,-4); $t=array(".jpg",".gif",".txt"); foreach($t as $ext){ if($file==$ext){ $x="correct file ext >>$file<<"; }else{ $x= "wrong file ext >>$file<<"; } } echo $x; ?> Quote Link to comment https://forums.phpfreaks.com/topic/91780-checking-for-proper-file-extensions-if-present-return-true/#findComment-470068 Share on other sites More sharing options...
laffin Posted February 19, 2008 Share Posted February 19, 2008 <?php header('Content-type: text/plain'); $filename='some.jPeG'; echo preg_match('/^.+\.(jpg|jpeg|gif|png)$/i',$filename)."\n"; $filename='some.gif'; echo preg_match('/^.+\.(jpg|jpeg|gif|png)$/i',$filename)."\n"; $filename='some.PNG'; echo preg_match('/^.+\.(jpg|jpeg|gif|png)$/i',$filename)."\n"; $filename='some.php.gif'; echo preg_match('/^.+\.(jpg|jpeg|gif|png)$/i',$filename)."\n"; $filename='some.php'; echo preg_match('/^.+\.(jpg|jpeg|gif|png)$/i',$filename)."\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/91780-checking-for-proper-file-extensions-if-present-return-true/#findComment-470138 Share on other sites More sharing options...
AndyB Posted February 19, 2008 Share Posted February 19, 2008 I want to check a file name, and if it has the file extension jpg, jpeg, JPG, JPEG, gif, GIF, png, PNG (and all other variations), then I want the code to return TRUE. Be very careful what you wish for. If you really want to be sure the file is an image, then run the getimagesize() function on it. Quote Link to comment https://forums.phpfreaks.com/topic/91780-checking-for-proper-file-extensions-if-present-return-true/#findComment-470140 Share on other sites More sharing options...
sKunKbad Posted February 19, 2008 Share Posted February 19, 2008 In the book "object oriented programming", the author has a function that checks that all files in a directory are images: function checkAllImages(){ $bln=true; $extension=''; $types= array('jpg', 'jpeg', 'gif', 'png'); foreach ($this->filearray as $value){ $extension = substr($value,(strpos($value, ".")+1)); $extension = strtolower($extension); if(!in_array($extension, $types)){ $bln = false; break; } } return $bln; $filearray is an array of all of the files in the directory. Quote Link to comment https://forums.phpfreaks.com/topic/91780-checking-for-proper-file-extensions-if-present-return-true/#findComment-470145 Share on other sites More sharing options...
laffin Posted February 19, 2008 Share Posted February 19, 2008 if u use strpos, it will find the first occurance use strrpos instead which finds the last occurance. in the example i use preg_match('/^.+\.(jpg|jpeg|gif|png)$/i',$filename) smallest way of accomplishing of what is asked, without extra support functions however as Andy states, an extension does not deem it a valid image. try using the image information routines to validate a valid image. Quote Link to comment https://forums.phpfreaks.com/topic/91780-checking-for-proper-file-extensions-if-present-return-true/#findComment-470151 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.