Jump to content

My Function Logic


graham23s

Recommended Posts

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.