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
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
Share on other sites

<?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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.