• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 398
  • Last Modified:

count number of .jpg's in a directory, sort in array to create links

I need to count number of images (.jpg) in a folder, sort them by name so I can dynamically create next and previous buttons in a gallery carousel.
0
gvilla23
Asked:
gvilla23
  • 2
1 Solution
 
VampireofdarknessCommented:
A nudge in the right direction would be opendir(): http://php.net/manual/en/function.opendir.php

You can traverse the folder to get the document names and types, separate only the jpg files into an array then do whatever else you want to do.
0
 
sbickerstaffCommented:
Here's some code I developed last year or the year before, based on the Zeneth Picure Gallery source code.  I've changed my code to return a count for you rather than what i was doing with it.  I haven't tested this version but the original checked and returned a list of all the jpgs or pngs



$pics = directory("../pic_directory/", "jpg,JPG,JPEG,jpeg,png,PNG");


function directory($dir, $ext) {
  $extlist = explode(",", $ext);
  $piccount = 0;
  
  // Open a known directory, and proceed to read its contents
  if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
      while (($file = readdir($dh)) !== false) {
        
        $pos = strrpos($file, '.');
        if(!$pos) {
          $ext = 'Unknown Filetype';
        }
        $pos++;
        $str = substr($file, $pos, strlen($file));
        $ext = $str;
        if (($file != "." && $file != "..") && (filetype($dir . $file) == "file") && (in_array($ext,$extlist) !== false)) {
          
          $piclist++;
        }
      }
      closedir($dh);
    }
  }
  return $piccount;
}

Open in new window

0
 
sbickerstaffCommented:
just noticed, that should be
$piccount++;
not
$piclist++;

thanks.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now