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

Posted on 2011-03-10
Last Modified: 2013-11-19
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.
Question by:gvilla23
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Expert Comment

ID: 35099124
A nudge in the right direction would be opendir():

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.

Accepted Solution

sbickerstaff earned 500 total points
ID: 35129162
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';
        $str = substr($file, $pos, strlen($file));
        $ext = $str;
        if (($file != "." && $file != "..") && (filetype($dir . $file) == "file") && (in_array($ext,$extlist) !== false)) {
  return $piccount;

Open in new window


Expert Comment

ID: 35167505
just noticed, that should be


Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
Make the most of your online learning experience.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question