File
[ class tree: File ] [ index: File ] [ all elements ]

Source for file Find.php

Documentation is available at Find.php

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Sterling Hughes <sterling@php.net>                           |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Find.php,v 1.15 2005/08/11 07:38:53 tuupola Exp $
  20. //
  21.  
  22. require_once 'PEAR.php';
  23.  
  24. /**
  25. *  Commonly needed functions searching directory trees
  26. *
  27. @access public
  28. @version $Id: Find.php,v 1.15 2005/08/11 07:38:53 tuupola Exp $
  29. @package File
  30. @author Sterling Hughes <sterling@php.net>
  31. */
  32. class File_Find
  33. {
  34.     /**
  35.      * internal dir-list
  36.      * @var array 
  37.      */
  38.     var $_dirs = array();
  39.  
  40.     /**
  41.      * found files
  42.      * @var array 
  43.      */
  44.     var $files = array();
  45.  
  46.     /**
  47.      * found dirs
  48.      * @var array 
  49.      */
  50.     var $directories = array();
  51.  
  52.     /**
  53.      * Search the current directory to find matches for the
  54.      * the specified pattern.
  55.      *
  56.      * @param string $pattern a string containing the pattern to search
  57.      *  the directory for.
  58.      *
  59.      * @param string $dirpath a string containing the directory path
  60.      *  to search.
  61.      *
  62.      * @param string $pattern_type a string containing the type of
  63.      *  pattern matching functions to use (can either be 'php' or
  64.      *  'perl').
  65.      *
  66.      * @return array containing all of the files and directories
  67.      *  matching the pattern or null if no matches
  68.      *
  69.      * @author Sterling Hughes <sterling@php.net>
  70.      * @access public
  71.      */
  72.     function &glob($pattern$dirpath$pattern_type 'php')
  73.     {
  74.         $dh @opendir($dirpath);
  75.  
  76.         if (!$dh{
  77.             $pe = PEAR::raiseError("Cannot open directory");
  78.             return $pe;
  79.         }
  80.  
  81.         $match_function File_Find::_determineRegex($pattern$pattern_type);
  82.         $matches = array();
  83.         while (false !== ($entry @readdir($dh))) {
  84.             if ($match_function($pattern$entry&&
  85.                 $entry != '.' && $entry != '..'{
  86.                 $matches[$entry;
  87.             }
  88.         }
  89.  
  90.         @closedir($dh);
  91.  
  92.         return (count($matches> 0$matches : null;
  93.     }
  94.  
  95.     /**
  96.      * Map the directory tree given by the directory_path parameter.
  97.      *
  98.      * @param string $directory contains the directory path that you
  99.      *  want to map.
  100.      *
  101.      * @return array a two element array, the first element containing a list
  102.      *  of all the directories, the second element containing a list of all the
  103.      *  files.
  104.      *
  105.      * @author Sterling Hughes <sterling@php.net>
  106.      * @access public
  107.      */
  108.     function &maptree($directory)
  109.     {
  110.  
  111.         /* if called statically */
  112.         if (!isset($this)  || !is_a($this"File_Find")) {
  113.             $obj &new File_Find();
  114.             return $obj->maptree($directory);
  115.         }
  116.       
  117.         /* clear the results just in case */
  118.         $this->files       = array();
  119.         $this->directories = array();
  120.  
  121.         /* strip out tailing / to be consistent */
  122.         $directory ereg_replace(DIRECTORY_SEPARATOR.'$'''$directory);
  123.  
  124.         $this->_dirs = array($directory);
  125.  
  126.         while (count($this->_dirs)) {
  127.             $dir array_pop($this->_dirs);
  128.             File_Find::_build($dir);
  129.             array_push($this->directories$dir);
  130.         }
  131.  
  132.         return array($this->directories$this->files);
  133.     }
  134.  
  135.     /**
  136.      * Map the directory tree given by the directory parameter.
  137.      *
  138.      * @param string $directory contains the directory path that you
  139.      *  want to map.
  140.      * @param integer $maxrecursion maximun number of folders to recursive
  141.      *  map
  142.      *
  143.      * @return array a multidimensional array containing all subdirectories
  144.      *  and their files. For example:
  145.      *
  146.      *  Array
  147.      *  (
  148.      *     [0] => file_1.php
  149.      *     [1] => file_2.php
  150.      *     [subdirname] => Array
  151.      *        (
  152.      *           [0] => file_1.php
  153.      *        )
  154.      *  )
  155.      *
  156.      * @author Mika Tuupola <tuupola@appelsiini.net>
  157.      * @access public
  158.      */
  159.     function &mapTreeMultiple($directory$maxrecursion = 0$count = 0)
  160.     {   
  161.         $retval = array();
  162.  
  163.         $count++;
  164.  
  165.         $directory .= DIRECTORY_SEPARATOR;
  166.         $dh opendir($directory);
  167.         while (false !== ($entry @readdir($dh))) {
  168.             if ($entry != '.' && $entry != '..'{
  169.                  array_push($retval$entry);
  170.             }
  171.         }
  172.  
  173.         closedir($dh);
  174.      
  175.         while (list($key$valeach($retval)) {
  176.             $path $directory $val;
  177.             $path str_replace(DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR,
  178.                                 DIRECTORY_SEPARATOR$path);
  179.       
  180.             if (!is_array($val&& is_dir($path)) {
  181.                 unset($retval[$key]);
  182.                 if ($maxrecursion == 0 || $count $maxrecursion{
  183.                     $retval[$valFile_Find::mapTreeMultiple($path
  184.                                     $maxrecursion$count);
  185.                 }
  186.             }
  187.         }
  188.  
  189.         return $retval;
  190.     }
  191.  
  192.     /**
  193.      * Search the specified directory tree with the specified pattern.  Return
  194.      * an array containing all matching files (no directories included).
  195.      *
  196.      * @param string $pattern the pattern to match every file with.
  197.      *
  198.      * @param string $directory the directory tree to search in.
  199.      *
  200.      * @param string $type the type of regular expression support to use, either
  201.      *  'php' or 'perl'.
  202.      *
  203.      * @param bool $fullpath whether the regex should be matched against the
  204.      *  full path or only against the filename
  205.      *
  206.      * @return array a list of files matching the pattern parameter in the the
  207.      *  directory path specified by the directory parameter
  208.      *
  209.      * @author Sterling Hughes <sterling@php.net>
  210.      * @access public
  211.      */
  212.     function &search($pattern$directory$type 'php'$fullpath = true)
  213.     {
  214.  
  215.         /* if called statically */
  216.         if (!isset($this)  || !is_a($this"File_Find")) {
  217.             $obj &new File_Find();
  218.             return $obj->search($pattern$directory$type$fullpath);
  219.         else {
  220.  
  221.             $matches = array();
  222.             list (,$files)  File_Find::maptree($directory);
  223.             $match_function File_Find::_determineRegex($pattern$type);
  224.  
  225.             reset($files);
  226.             while (list(,$entryeach($files)) {
  227.                 if ($match_function($pattern
  228.                                     $fullpath $entry basename($entry))) {
  229.                     $matches[$entry;
  230.                 }
  231.             }
  232.         }
  233.  
  234.         return ($matches);
  235.     }
  236.  
  237.     /**
  238.      * Determine whether or not a variable is a PEAR error
  239.      *
  240.      * @param object PEAR_Error $var the variable to test.
  241.      *
  242.      * @return boolean returns true if the variable is a PEAR error, otherwise
  243.      *  it returns false.
  244.      * @access public
  245.      */
  246.     function isError(&$var)
  247.     {
  248.         return PEAR::isError($var);
  249.     }
  250.  
  251.     /**
  252.      * internal function to build singular directory trees, used by
  253.      * File_Find::maptree()
  254.      *
  255.      * @param string $directory name of the directory to read
  256.      * @return void 
  257.      */
  258.     function _build($directory)
  259.     {
  260.  
  261.         $dh @opendir($directory);
  262.  
  263.         if (!$dh{
  264.             $pe = PEAR::raiseError("Cannot open directory");
  265.             return $pe;
  266.         }
  267.  
  268.         while (false !== ($entry @readdir($dh))) {
  269.             if ($entry != '.' && $entry != '..'{
  270.  
  271.                 $entry $directory.DIRECTORY_SEPARATOR.$entry;
  272.                 $entry str_replace(DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR,
  273.                                      DIRECTORY_SEPARATOR$entry);
  274.  
  275.                 if (is_dir($entry)) {
  276.                     array_push($this->_dirs$entry);
  277.                 else {
  278.                     array_push($this->files$entry);
  279.                 }
  280.             }
  281.         }
  282.  
  283.         @closedir($dh);
  284.     }
  285.  
  286.     /**
  287.      * internal function to determine the type of regular expression to
  288.      * use, implemented by File_Find::glob() and File_Find::search()
  289.      *
  290.      * @param string $type given RegExp type
  291.      * @return string kind of function ( "eregi", "ereg" or "preg_match") ;
  292.      *
  293.      */
  294.     function _determineRegex($pattern$type)
  295.     {
  296.         if (!strcasecmp($type'perl')) {
  297.             $match_function 'preg_match';
  298.         else if (!strcasecmp(substr($pattern-2)'/i')) {
  299.             $match_function 'eregi';
  300.         else {
  301.             $match_function 'ereg';
  302.         }
  303.  
  304.         return $match_function;
  305.     }
  306. }
  307.  
  308. /*
  309.  * Local variables:
  310.  * tab-width: 4
  311.  * c-basic-offset: 4
  312.  * End:
  313.  */
  314.  
  315. ?>

Documentation generated on Mon, 11 Mar 2019 14:15:37 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.