Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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.
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
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…

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