Solved

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

Posted on 2011-03-10
3
385 Views
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.
0
Comment
Question by:gvilla23
  • 2
3 Comments
 
LVL 9

Expert Comment

by:Vampireofdarkness
Comment Utility
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
 
LVL 3

Accepted Solution

by:
sbickerstaff earned 500 total points
Comment Utility
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
 
LVL 3

Expert Comment

by:sbickerstaff
Comment Utility
just noticed, that should be
$piccount++;
not
$piclist++;

thanks.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Read about why website design really matters in today's demanding market.
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
The viewer will learn how to dynamically set the form action using jQuery.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now