Results 1 to 5 of 5

Thread: [RESOLVED] Checking File Extension

  1. #1

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Resolved [RESOLVED] Checking File Extension

    i have some code that checks to see if the file ext is a valid picture that we support. The problem is that it is a valid extension, but it returns that it isnt.

    here is my code:

    PHP Code:
    $image $_FILES['image_file']['tmp_name']; 
    $memberid $_SESSION['member_id'];
    $pos strrpos($image'.'); 
    $ext substr($image$posstrlen($image) - $pos);
    if(
    $ext!="jpg"&&$ext!="jpeg"&&$ext!="png"&&$ext!="tif"&&$ext!="gif") {
               
    $error[2] = "The file you selected is not supported by our system.";

    My usual boring signature: Something

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Checking File Extension

    I can't really read that code, but try this instead.

    PHP Code:
    $valid_extensions = array(
      
    'jpg',
      
    'jpeg',
      
    'png',
      
    'tif',
      
    'gif'
    );

    if (!
    in_array(pathinfo($imagePATHINFO_EXTENSION), $valid_extensions))
      
    $error[2] = 'The file you selected is not supported by our system.'

    You should bear in mind that extensions aren't reliable; you should also perform some sort of validation upon the file itself. For images, this can be done by attempting to load it using the GD library.

  3. #3
    Hyperactive Member NPassero's Avatar
    Join Date
    May 2007
    Location
    NJ
    Posts
    272

    Re: Checking File Extension

    i would just use the explode function..
    just recently did this:
    Code:
    $file = explode(".", $_FILES['image_file']['tmp_name'] );
    // returns 
    // $file[0] = filename 
    // $file[1] = .fileEXT
    $extension = $first[1];
    // Then compare to the available extensions
    Last edited by NPassero; Jun 7th, 2007 at 10:45 AM. Reason: fixed errors.

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Checking File Extension

    And what if the filename contains multiple dots?

    Besides, you've used $file[0] twice in your code (should the second one be $file[1]?), and $first[1] after that (where does $first come from?). Oh, and you're missing a comma between the arguments for explode().

    If there is a function available that does what you need, it is advisable to use that, rather than use other functions to achieve the same thing. For one, it makes the code more readable; you don't have to pepper it with comments such as 'explode the filename to get the file extension'. It also is shorter and has less chance of error.

  5. #5

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: [RESOLVED] Checking File Extension

    thanks you guys for your help!
    My usual boring signature: Something

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width