Solved

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

Posted on 2011-03-10
3
389 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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb wildfly example 2 17
php56-php-mcrypt for rhel7 php56 1 44
Link failure 16 32
Make a website not indexable by google 6 11
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

809 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