bikerabhinav

Image Extract PHP

Jan 28th, 2013
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.15 KB | None | 0 0
  1. <?php
  2. /*==== PHP Script to get images from a folder and it's subfolders ======== */
  3.  
  4. // Change this to whichever folder you want to scan for images =========*/
  5. $imgdir = "./images";
  6.  
  7. // You can modify this in case you need a different image extension
  8. $strExt = "jpg";
  9.  
  10. // This is the full match pattern based upon your selections above
  11. $pattern = "*.".$strExt;
  12.  
  13. /*==============================================================================*/
  14.  
  15. function GetFiles($files, $search) {
  16.     // Split to name and filetype
  17.     if(strpos($search,".")) {        
  18.         $baseExpr=substr($search,0,strpos($search,"."));
  19.         $typeexp=substr($search,strpos($search,".")+1,strlen($search));
  20.     } else {
  21.         $baseExpr=$search;
  22.         $typeexp="";
  23.     }
  24.     // Escape all regexp Characters
  25.     $baseExpr=preg_quote($baseExpr);
  26.     $typeexp=preg_quote($typeexp);
  27.  
  28.     // Allow ? and *
  29.     $baseExpr=str_replace(array("\*","\?"), array(".*","."), $baseExpr);
  30.     $typeexp=str_replace(array("\*","\?"), array(".*","."), $typeexp);
  31.        
  32.     // Search for Matches
  33.     $i=0;    
  34.     foreach($files as $file) {
  35.         $fname=basename($file);
  36.  
  37.         if(strpos($fname,".")) {
  38.             $base=substr($fname,0,strpos($fname,"."));
  39.             $type=substr($fname,strpos($fname,".")+1,strlen($fname));
  40.         } else {
  41.             $base=$fname;
  42.             $type="";
  43.         }
  44.  
  45.         if(preg_match("/^".$baseExpr."$/i",$base) && preg_match("/^".$typeexp."$/i",$type))  {
  46.             $matches[$i]=$file;
  47.             $i++;
  48.         }
  49.     }
  50.     return $matches;
  51. }
  52.  
  53. // Recursively returns all Files contained in given dir, including subdirs
  54. function GetImages($dir,$files=array()) {
  55.     if(!($res=opendir($dir))) exit("$dir doesn't exist!");
  56.     while(($file=readdir($res))==TRUE) {
  57.         if($file!="." && $file!="..")
  58.             if(is_dir("$dir/$file"))
  59.                 $files=GetImages("$dir/$file",$files);
  60.             else
  61.                 array_push($files,"$dir/$file");
  62.     }
  63.     closedir($res);
  64.     return $files;
  65. }
  66.  
  67. $matches = GetFiles(GetImages($imgdir),$pattern);
  68.  
  69. echo '<pre>';
  70. var_dump($matches);
  71. echo '</pre>';
  72. ?>
Advertisement
Add Comment
Please, Sign In to add comment