Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*==== PHP Script to get images from a folder and it's subfolders ======== */
- // Change this to whichever folder you want to scan for images =========*/
- $imgdir = "./images";
- // You can modify this in case you need a different image extension
- $strExt = "jpg";
- // This is the full match pattern based upon your selections above
- $pattern = "*.".$strExt;
- /*==============================================================================*/
- function GetFiles($files, $search) {
- // Split to name and filetype
- if(strpos($search,".")) {
- $baseExpr=substr($search,0,strpos($search,"."));
- $typeexp=substr($search,strpos($search,".")+1,strlen($search));
- } else {
- $baseExpr=$search;
- $typeexp="";
- }
- // Escape all regexp Characters
- $baseExpr=preg_quote($baseExpr);
- $typeexp=preg_quote($typeexp);
- // Allow ? and *
- $baseExpr=str_replace(array("\*","\?"), array(".*","."), $baseExpr);
- $typeexp=str_replace(array("\*","\?"), array(".*","."), $typeexp);
- // Search for Matches
- $i=0;
- foreach($files as $file) {
- $fname=basename($file);
- if(strpos($fname,".")) {
- $base=substr($fname,0,strpos($fname,"."));
- $type=substr($fname,strpos($fname,".")+1,strlen($fname));
- } else {
- $base=$fname;
- $type="";
- }
- if(preg_match("/^".$baseExpr."$/i",$base) && preg_match("/^".$typeexp."$/i",$type)) {
- $matches[$i]=$file;
- $i++;
- }
- }
- return $matches;
- }
- // Recursively returns all Files contained in given dir, including subdirs
- function GetImages($dir,$files=array()) {
- if(!($res=opendir($dir))) exit("$dir doesn't exist!");
- while(($file=readdir($res))==TRUE) {
- if($file!="." && $file!="..")
- if(is_dir("$dir/$file"))
- $files=GetImages("$dir/$file",$files);
- else
- array_push($files,"$dir/$file");
- }
- closedir($res);
- return $files;
- }
- $matches = GetFiles(GetImages($imgdir),$pattern);
- echo '<pre>';
- var_dump($matches);
- echo '</pre>';
- ?>
Advertisement
Add Comment
Please, Sign In to add comment