Using an array to check for valid file extensions in PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Anthony2oo5
    New Member
    • Jun 2007
    • 26

    Using an array to check for valid file extensions in PHP

    Hey there,

    I'm trying to write a script for uploading images but only with certain file extensions. Currently I have it working by:

    [PHP]if (($FileExt = "jpg") || ($FileExt = "gif"))[/PHP]

    ect, but is there a way i can do:

    [PHP]$allowed = array("jpg", "gif");
    if $allowed[/PHP]

    I tried that but the if always turned out false.

    What would be the correct way of doing this?

    Thanks for your time.
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Changed thread title to better describe the problem.

    Heya, Anthony2oo5.

    The way I would do it:

    [code=php]
    $allowed = array(
    'jpg' => true,
    'gif' => true,
    'etc' => true
    );

    if($allowed[$FileExt]) {
    .
    .
    .
    }
    [/code]

    The idea is that $allowed[$FileExt] will be true if $FileExt is an existing key in $allowed, but it will evaluate to false (null == false) if $FileExt does not exist in $allowed.

    Comment

    • Asterik
      New Member
      • Jun 2007
      • 2

      #3
      One way I use is:

      [code=php]
      $allowed = array(".jpg", ".gif");
      if(in_array(get FileExt())) ....
      [/code]

      and here's getFileExt() function:
      [code=php]
      function getFileExt($fil efield) {
      /*************** *************** *************** *****
      $pos: return the position of "." in file name
      $filefield: name of your file upload file
      *************** *************** *************** ****/
      $pos = strpos(basename ($_FILES[$filefield]['name']),".");
      $fileext = substr(basename ($_FILES[$filefield]['name']),$pos,strlen($ _FILES[$filefield]['name']));
      return $fileext;
      }
      [/code]
      Last edited by Atli; Jun 18 '07, 12:41 AM. Reason: Added code tags

      Comment

      • Anthony2oo5
        New Member
        • Jun 2007
        • 26

        #4
        Hey thanks both I will try these solutions later.

        I have a function for getting the file extension. I used one similar to what you have wrote but it didn't work for reading the file extension inside zip files, so I made another one:

        [PHP] function GetFileExt($fil ename) {
        return end(explode("." , $filename));
        }[/PHP]

        Thanks again, I will come back and update with my progress.

        Comment

        • Mathenge

          #5
          Thanks you helped alot i used this and working
          Code:
          //check valid file extensions only jpg,gif or png
          function checkvalidext($ext) {
            $allowed = array(
               	'jpg'    =>    true,
              	'gif'    =>    true,
              	'png'    =>    true
           );
             if($allowed[$ext])
              {
          	return true;
          	} 
             else {
             return false;
             }   ;
             }
           //check valid file extensions only jpg,gif or png
          function checkfilesize($x) {
          
             if($x< 1024000)
              {	return true;} 
             else { return false;
             } 
          Then called these functions as
            $validfile = checkvalidext($ext);
            $validfilesize = checkfilesize($filesize);
          Last edited by Dormilich; Nov 13 '10, 07:57 AM. Reason: please use [CODE] [/CODE] tags when posting code

          Comment

          Working...