Hi

Is it possible to filter the array produced by scandir?

I know it can be done with opendir and readdir if/loop functions

Here is the output from my scandir function
Array ( [0] => . [1] => .. [2] => pic_0.jpg [3] => pic_1.png [4] => pic_2.txt [5] => pic_3.jpg [6] => pic_4.gif )

I only want the files with the extensions "jpg", "png", "jpeg", "gif"

Thanks

    Thanks for the reply bogu

    I've seen it but not used glob before, maybe because the examples on php.net are confusing by going on about file size.

    So would this work

    $Pics="my/folder/"
    $Pictures=glob('$Pics.jpg;.png;.jpeg;.gif') ?>

    Would I get this ?

    Array ( [0] => pic_0.jpg [1] => pic_1.png [2] => pic_2.gif [3] => pic_3.jpeg )

    I can't connect to me server at the mo.

      $Pictures = glob($Pics . '{*.jpg,*.png,*.jpeg,*.gif}', GLOB_BRACE);

      Keep in mind that this is case-sensitive.

        Thanks for that pointer Installer. I'll change the extensions to include capitals but I think that my upload script only allows lower case extensions.

        [QUOTE]// List of our known photo types
        $known_photo_types = array( 
        					'image/pjpeg' => 'jpg',
        					'image/jpeg' => 'jpg',
        					'image/gif' => 'gif',
        					'image/bmp' => 'bmp',
        					'image/x-png' => 'png'
        				);
        
        // GD Function List
        $gd_function_suffix = array( 
        					'image/pjpeg' => 'JPEG',
        					'image/jpeg' => 'JPEG',
        					'image/gif' => 'GIF',
        					'image/bmp' => 'WBMP',
        					'image/x-png' => 'PNG'
        				);
        
        // Fetch the photo array sent by [/QUOTE] 

        Hope I get time this week-end between the matches

          Here's a generic function that will search for an array of extensions, case-insensitive (well, not completely case-insensitive; files with mixed-case extensions won't be found):

          function glob_ext_i($exts, $dir = './')
          {
              if (substr($dir, -1) != '/') {
                  $dir .= '/';
              }
              $glob_str = $dir . '*.{';
              foreach ($exts as $ext) {
                  $glob_str .= strtolower($ext) . ',' . strtoupper($ext) . ',';
              }
              $glob_str = rtrim($glob_str, ',') . '}';
              return glob($glob_str, GLOB_BRACE);
          }

          Example:

          $Pics = '..';
          $exts = array('JPG', 'png', 'jpeg', 'gif');
          if ($glob = glob_ext_i($exts, $Pics)) {
              foreach ($glob as $file) {
                  echo $file . '<br />';
              }
          }

            Thanks for your help

            Me glob works great. I got the files into an array and passed them from php to javascript then back into an array for my slideshow.

            Thanks for your help. Now onto the next stage, sending an email from php.

              Write a Reply...