Solved

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

Posted on 2011-03-10
3
390 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
ID: 35099124
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
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';
        }
        $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
ID: 35167505
just noticed, that should be
$piccount++;
not
$piclist++;

thanks.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
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…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

685 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