Jump to content

Help WIth An Array


refiking

Recommended Posts

I want to have an array of extension types that are acceptable.  Currently, the method I am using will only allow me to check 1.  What should I do differently?

 

$ext = substr($file_name, strrpos($file_name, '.') + 1);
if($ext != 'jpg'){ 
$exterror++; 
}
if($exterror > 0){
    echo "Error:File type not supported.";
    return;
}

Link to comment
Share on other sites

try this:

 

$ext_array("jpg", "png"); // so on and so forth
$ext = substr($file_name, strrpos($file_name, '.') + 1);
if(!in_array($ext, $ext_array)){ 
$exterror++; 
}
if($exterror > 0){
    echo "Error:File type not supported.";
    return;
}

 

hope this helps.

 

bluejay,

Link to comment
Share on other sites

You can also do it like this (using the built in function to get the extension):

 

$allowed = array("jpg", "gif", "bmp");
if(in_array(pathinfo($path, PATHINFO_EXTENSION), $allowed)) {
    // valid
} else {
    // not
}

Link to comment
Share on other sites

@projectFear:

well, its not entirely my code. I just assume his code were correct and added my code on it. I'm kinda lazy writing a new code over something that the user didn't complained about.  ;)

 

True enough, but hopefully the OP can look at modified (and cleaned) code and see any improvements (or maybe it got worse).

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.