graham23s Posted June 26, 2008 Share Posted June 26, 2008 Hi Guys, i'm trying to understand the logic here in the code i have written: function: <?php function is_valid_image_file($var_website_image_name) { // this takes away - 4 characters from the end of the file $ext = substr("$var_website_image_name", - 4); // is the extension allowed if ($ext != '.jpg' || $ext != '.gif' || $ext != '.png') { return false; } else { return true; } } ?> calling the function: <?php // check its a valid image if (is_valid_image_file($var_website_image_name)) { $errors[] = "The file you uploaded doesn't appear to be an image file."; } ?> if i put ! infront of if (is_valid_image_file($var_website_image_name)) it doesn't work the waqy i thought it would, could anyone explain the logic to me so i know for future reference lol cheers Graham Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted June 26, 2008 Share Posted June 26, 2008 I think the problem is that you are using 'or' in your if statement within your function, rather than 'and'. I assume you wanted to check to make sure the extension was one of those 3, in which case you need and. If you think about your if statement, it will always be true. You might find it easier to use an array and use the in_array() function, however. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted June 26, 2008 Share Posted June 26, 2008 <?php function is_valid_image_file($var_website_image_name) { return in_array( substr($var_website_image_name,strrpos($var_website_image_name,'.')+1), array('jpg','gif','png') ); } ?> Quote Link to comment Share on other sites More sharing options...
graham23s Posted June 26, 2008 Author Share Posted June 26, 2008 Thanks guys, that does seem a better way of doing things! thanks again guys Graham Quote Link to comment Share on other sites More sharing options...
Jabop Posted June 26, 2008 Share Posted June 26, 2008 Rhodesa: that seems like a bad way to get the extension. I prefer to use: <?php $img="blahblah.jpg"; $img=explode(".", $img); $ext=array_pop($img); ?> Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted June 26, 2008 Share Posted June 26, 2008 I agree. The previous method relies on the the extension being just four letters. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted June 26, 2008 Share Posted June 26, 2008 Rhodesa: that seems like a bad way to get the extension. I prefer to use: <?php $img="blahblah.jpg"; $img=explode(".", $img); $ext=array_pop($img); ?> both would work....why is this way any better? Quote Link to comment Share on other sites More sharing options...
Jabop Posted June 26, 2008 Share Posted June 26, 2008 Simplicity and flexibility. jpegs are still jpgs, but they have 4 chars. It's still a valid extension, shouldn't exclude it. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted June 26, 2008 Share Posted June 26, 2008 substr($var_website_image_name,strrpos($var_website_image_name,'.')+1) get's everything AFTER the last period...regardless of length Quote Link to comment Share on other sites More sharing options...
Jabop Posted June 26, 2008 Share Posted June 26, 2008 I was speaking of your array of allowed types, but that's beside the point. The method I posted is simpler in my opinion. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted June 26, 2008 Share Posted June 26, 2008 ah...i was just using the extensions they had originally posted...another good way to make sure it's an image (if you have gd enabled) is with: <?php function valid_image($image_path){ return (is_file($image_path) && @getimagesize($image_path)); } ?> Quote Link to comment Share on other sites More sharing options...
Jabop Posted June 26, 2008 Share Posted June 26, 2008 Another way to verify: http://us.php.net/manual/en/function.exif-imagetype.php Quote Link to comment Share on other sites More sharing options...
br0ken Posted June 28, 2008 Share Posted June 28, 2008 substr($var_website_image_name,strrpos($var_website_image_name,'.')+1) get's everything AFTER the last period...regardless of length This would be the technique I'd used to extract the file extension. While you might argue both are just as easy this method requires one line of coding where as the other requires two. Quote Link to comment 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.