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 Link to comment https://forums.phpfreaks.com/topic/112072-my-function-logic/ 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. Link to comment https://forums.phpfreaks.com/topic/112072-my-function-logic/#findComment-575289 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') ); } ?> Link to comment https://forums.phpfreaks.com/topic/112072-my-function-logic/#findComment-575291 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 Link to comment https://forums.phpfreaks.com/topic/112072-my-function-logic/#findComment-575297 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); ?> Link to comment https://forums.phpfreaks.com/topic/112072-my-function-logic/#findComment-575313 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. Link to comment https://forums.phpfreaks.com/topic/112072-my-function-logic/#findComment-575315 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? Link to comment https://forums.phpfreaks.com/topic/112072-my-function-logic/#findComment-575320 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. Link to comment https://forums.phpfreaks.com/topic/112072-my-function-logic/#findComment-575324 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 Link to comment https://forums.phpfreaks.com/topic/112072-my-function-logic/#findComment-575329 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. Link to comment https://forums.phpfreaks.com/topic/112072-my-function-logic/#findComment-575335 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)); } ?> Link to comment https://forums.phpfreaks.com/topic/112072-my-function-logic/#findComment-575340 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 Link to comment https://forums.phpfreaks.com/topic/112072-my-function-logic/#findComment-575347 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. Link to comment https://forums.phpfreaks.com/topic/112072-my-function-logic/#findComment-576411 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.