Advertisement
Strider64

Grab images from any directory

Nov 9th, 2017
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.79 KB | None | 0 0
  1. <?php
  2.  
  3. $directory = "work"; // Directory where the images are located:
  4. $myFiles = scandir($directory); // Grab a listing of you files:
  5. $allowedExts = ["jpg", "jpeg", "gif", "png"]; // Allowed image extensions:
  6. for ($i = 0; $i < count($myFiles); $i++) {
  7.     /*
  8.      * Obmit the . and .. in the directory
  9.      */
  10.     if ($myFiles[$i] !== '.' && $myFiles[$i] !== '..') {
  11.         $ext = strtolower(pathinfo($myFiles[$i], PATHINFO_EXTENSION)); // Grab the extension of the file(s):
  12.         if (in_array($ext, $allowedExts)) {
  13.             $path_parts = pathinfo($myFiles[$i]);
  14.             $file_name[] = $path_parts['filename'];
  15.         }
  16.        
  17.     }
  18. }
  19.  
  20. $num_files = count($file_name); // Number of images in lib/images/slideshow directory:
  21.  
  22. echo "<pre>" . print_r($file_name, 1) . "</pre>";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement