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

Source for file Util.php

Documentation is available at Util.php

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PEAR :: File :: Util                                                 |
  4. // +----------------------------------------------------------------------+
  5. // | This source file is subject to version 3.0 of the PHP license,       |
  6. // | that is available at http://www.php.net/license/3_0.txt              |
  7. // | If you did not receive a copy of the PHP license and are unable      |
  8. // | to obtain it through the world-wide-web, please send a note to       |
  9. // | license@php.net so we can mail you a copy immediately.               |
  10. // +----------------------------------------------------------------------+
  11. // | Copyright (c) 2005 Michael Wallner <mike@iworks.at>                  |
  12. // +----------------------------------------------------------------------+
  13. //
  14. // $Id: Util.php,v 1.6 2005/01/12 08:46:00 mike Exp $
  15.  
  16. /**#@+
  17.  * Sorting Constants
  18.  */
  19. define('FILE_SORT_NONE',    0);
  20. define('FILE_SORT_REVERSE'1);
  21. define('FILE_SORT_NAME',    2);
  22. define('FILE_SORT_SIZE',    4);
  23. define('FILE_SORT_DATE',    8);
  24. /**#@-*/
  25.  
  26. /**#@+
  27.  * Listing Constants
  28.  */
  29. define('FILE_LIST_FILES',   1);
  30. define('FILE_LIST_DIRS',    2);
  31. define('FILE_LIST_DOTS',    4);
  32. define('FILE_LIST_ALL',     FILE_LIST_FILES | FILE_LIST_DIRS | FILE_LIST_DOTS);
  33. /**#@-*/
  34.  
  35. /** 
  36.  * File_Util
  37.  *
  38.  * File and directory utility functions.
  39.  */
  40. class File_Util
  41. {
  42.     /**
  43.      * Returns a string path built from the array $pathParts. Where a join
  44.      * occurs multiple separators are removed. Joins using the optional
  45.      * separator, defaulting to the PHP DIRECTORY_SEPARATOR constant.
  46.      * 
  47.      * @static
  48.      * @access  public
  49.      * @param   array   $parts Array containing the parts to be joined
  50.      * @param   string  $separator The directory seperator
  51.      */
  52.     function buildPath($parts$separator = DIRECTORY_SEPARATOR)
  53.     {
  54.         /*
  55.          * @FIXXME: maybe better use foreach
  56.          */
  57.         
  58.         for ($i = 0$c count($parts)$i $c$i++{
  59.             if (    !strlen($parts[$i]|| 
  60.                     preg_match('/^'preg_quote($separator.'+$/'$parts[$i])) {
  61.                 unset($parts[$i]);
  62.             elseif (0 == $i{
  63.                 $parts[$irtrim($parts[$i]$separator);
  64.             elseif ($c - 1 == $i{
  65.                 $parts[$iltrim($parts[$i]$separator);
  66.             else {
  67.                 $parts[$itrim($parts[$i]$separator);
  68.             
  69.         
  70.         return implode($separator$parts);
  71.     
  72.  
  73.     /**
  74.      * Returns a path without leading / or C:\. If this is not
  75.      * present the path is returned as is.
  76.      * 
  77.      * @static
  78.      * @access  public
  79.      * @param   string  $path The path to be processed
  80.      * @return  string  The processed path or the path as is
  81.      */
  82.     function skipRoot($path)
  83.     {
  84.         if (File_Util::isAbsolute($path)) {
  85.             if (OS_WINDOWS{
  86.                 return substr($path$path{3== '\\' ? 4 : 3);
  87.             }
  88.             return ltrim($path'/');
  89.         }
  90.         return $path;
  91.     
  92.  
  93.     /**
  94.      * Returns the temp directory according to either the TMP, TMPDIR, or
  95.      * TEMP env variables. If these are not set it will also check for the
  96.      * existence of /tmp, %WINDIR%\temp
  97.      * 
  98.      * @static
  99.      * @access  public
  100.      * @return  string  The system tmp directory
  101.      */
  102.     function tmpDir()
  103.     {
  104.         if (OS_WINDOWS{
  105.             if (isset($_ENV['TEMP'])) {
  106.                 return $_ENV['TEMP'];
  107.             }
  108.             if (isset($_ENV['TMP'])) {
  109.                 return $_ENV['TMP'];
  110.             }
  111.             if (isset($_ENV['windir'])) {
  112.                 return $_ENV['windir''\\temp';
  113.             }
  114.             if (isset($_ENV['SystemRoot'])) {
  115.                 return $_ENV['SystemRoot''\\temp';
  116.             }
  117.             if (isset($_SERVER['TEMP'])) {
  118.                 return $_SERVER['TEMP'];
  119.             }
  120.             if (isset($_SERVER['TMP'])) {
  121.                 return $_SERVER['TMP'];
  122.             }
  123.             if (isset($_SERVER['windir'])) {
  124.                 return $_SERVER['windir''\\temp';
  125.             }
  126.             if (isset($_SERVER['SystemRoot'])) {
  127.                 return $_SERVER['SystemRoot''\\temp';
  128.             }
  129.             return '\temp';
  130.         }
  131.         if (isset($_ENV['TMPDIR'])) {
  132.             return $_ENV['TMPDIR'];
  133.         }
  134.         if (isset($_SERVER['TMPDIR'])) {
  135.             return $_SERVER['TMPDIR'];
  136.         }
  137.         return '/tmp';
  138.     }
  139.  
  140.     /**
  141.      * Returns a temporary filename using tempnam() and File::tmpDir().
  142.      * 
  143.      * @static
  144.      * @access  public
  145.      * @param   string  $dirname Optional directory name for the tmp file
  146.      * @return  string  Filename and path of the tmp file
  147.      */
  148.     function tmpFile($dirname = null)
  149.     {
  150.         if (!isset($dirname)) {
  151.             $dirname = File_Util::tmpDir();
  152.         
  153.         return tempnam($dirname'temp.');
  154.     
  155.  
  156.     /**
  157.      * Returns boolean based on whether given path is absolute or not.
  158.      * 
  159.      * @static
  160.      * @access  public
  161.      * @param   string  $path Given path
  162.      * @return  boolean True if the path is absolute, false if it is not
  163.      */
  164.     function isAbsolute($path)
  165.     {
  166.         if (preg_match('/\.\./'$path)) {
  167.             return false;
  168.         
  169.         if (OS_WINDOWS{
  170.             return preg_match('/^[a-zA-Z]:(\\\|\/)/'$path);
  171.         }
  172.         return ($path{0== '/'|| ($path{0== '~');
  173.     
  174.  
  175.     /**
  176.      * Get path relative to another path
  177.      *
  178.      * @static
  179.      * @access  public
  180.      * @return  string 
  181.      * @param   string  $path 
  182.      * @param   string  $root 
  183.      * @param   string  $separator 
  184.      */
  185.     function relativePath($path$root$separator = DIRECTORY_SEPARATOR)
  186.     {
  187.         $path = File_Util::realpath($path$separator);
  188.         $root = File_Util::realpath($root$separator);
  189.         $dirs explode($separator$path);
  190.         $comp explode($separator$root);
  191.         
  192.         if (OS_WINDOWS{
  193.             if (strcasecmp($dirs[0]$comp[0])) {
  194.                 return $path;
  195.             }
  196.             unset($dirs[0]$comp[0]);
  197.         }
  198.         
  199.         foreach ($comp as $i => $part{
  200.             if (isset($dirs[$i]&& $part == $dirs[$i]{
  201.                 unset($dirs[$i]$comp[$i]);
  202.             else {
  203.                 break;
  204.             }
  205.         }
  206.          
  207.         return str_repeat('..' $separatorcount($comp)) implode($separator$dirs);
  208.     }
  209.  
  210.     /**
  211.      * Get real path (works with non-existant paths)
  212.      *
  213.      * @static
  214.      * @access  public
  215.      * @return  string 
  216.      * @param   string  $path 
  217.      * @param   string  $separator 
  218.      */
  219.     function realPath($path$separator = DIRECTORY_SEPARATOR)
  220.     {
  221.         if (!strlen($path)) {
  222.             return $separator;
  223.         }
  224.         
  225.         $drive '';
  226.         if (OS_WINDOWS{
  227.             $path preg_replace('/[\\\\\/]/'$separator$path);
  228.             if (preg_match('/([a-zA-Z]\:)(.*)/'$path$matches)) {
  229.                 $drive $matches[1];
  230.                 $path  $matches[2];
  231.             else {
  232.                 $cwd   getcwd();
  233.                 $drive substr($cwd02);
  234.                 if ($path{0!== $separator{0}{
  235.                     $path  substr($cwd3$separator $path;
  236.                 }
  237.             }
  238.         elseif ($path{0!== $separator{
  239.             $cwd  getcwd();
  240.             $path $cwd $separator 
  241.                 File_Util::relativePath($path$cwd$separator);
  242.         }
  243.         
  244.         $dirStack = array();
  245.         foreach (explode($separator$pathas $dir{
  246.             if (strlen($dir&& $dir !== '.'{
  247.                 if ($dir == '..'{
  248.                     array_pop($dirStack);
  249.                 else {
  250.                     $dirStack[$dir;
  251.                 }
  252.             }
  253.         }
  254.         
  255.         return $drive $separator implode($separator$dirStack);
  256.     }
  257.  
  258.     /**
  259.      * Check whether path is in root path
  260.      *
  261.      * @static
  262.      * @access  public
  263.      * @return  bool 
  264.      * @param   string  $path 
  265.      * @param   string  $root 
  266.      */
  267.     function pathInRoot($path$root)
  268.     {
  269.         static $realPaths = array();
  270.         
  271.         if (!isset($realPaths[$root])) {
  272.             $realPaths[$root= File_Util::realPath($root);
  273.         }
  274.         
  275.         return false !== strstr(File_Util::realPath($path)$realPaths[$root]);
  276.     }
  277.  
  278.     /**
  279.      * List Directory
  280.      * 
  281.      * @static
  282.      * @access  public
  283.      * @return  array 
  284.      * @param   string  $path 
  285.      * @param   int     $list 
  286.      * @param   int     $sort 
  287.      */
  288.     function listDir($path$list = FILE_LIST_ALL$sort = FILE_SORT_NONE)
  289.     {
  290.         if (!strlen($path|| !is_dir($path)) {
  291.             return null;
  292.         }
  293.         
  294.         $entries = array();
  295.         for ($dir dir($path); false !== $entry $dir->read(){
  296.             if ($list FILE_LIST_DOTS || $entry{0!== '.'{
  297.                 $isRef ($entry === '.' || $entry === '..');
  298.                 $isDir $isRef || is_dir($path .'/'$entry);
  299.                 if (    (!$isDir && $list FILE_LIST_FILES)   ||
  300.                         ($isDir  && $list FILE_LIST_DIRS)) {
  301.                     $entries[= (object) array(
  302.                         'name'  => $entry,
  303.                         'size'  => $isDir ? null : filesize($path .'/'$entry),
  304.                         'date'  => filemtime($path .'/'$entry),
  305.                     );
  306.                 }
  307.             }
  308.         }
  309.         $dir->close();
  310.         
  311.         if ($sort{
  312.             $entries = File_Util::sortFiles($entries$sort);
  313.         }
  314.         
  315.         return $entries;
  316.     }
  317.     
  318.     /**
  319.      * Sort Files
  320.      * 
  321.      * @static
  322.      * @access  public
  323.      * @return  array 
  324.      * @param   array   $files 
  325.      * @param   int     $sort 
  326.      */
  327.     function sortFiles($files$sort)
  328.     {
  329.         if (!$files{
  330.             return array();
  331.         }
  332.         
  333.         if (!$sort{
  334.             return $files;
  335.         }
  336.         
  337.         if ($sort == 1{
  338.             return array_reverse($files);
  339.         }
  340.         
  341.         $sortFlags = array(
  342.             FILE_SORT_NAME => SORT_STRING
  343.             FILE_SORT_DATE => SORT_NUMERIC
  344.             FILE_SORT_SIZE => SORT_NUMERIC,
  345.         );
  346.         
  347.         foreach ($files as $file{
  348.             $names[$file['name'];
  349.             $sizes[$file['size'];
  350.             $dates[$file['date'];
  351.         }
  352.     
  353.         if ($sort FILE_SORT_NAME{
  354.             $r &$names;
  355.         elseif ($sort FILE_SORT_DATE{
  356.             $r &$dates;
  357.         elseif ($sort FILE_SORT_SIZE{
  358.             $r &$sizes;
  359.         }
  360.         
  361.         if ($sort FILE_SORT_REVERSE{
  362.             arsort($r$sortFlags[$sort ~1]);
  363.         else {
  364.             asort($r$sortFlags[$sort]);
  365.         }
  366.     
  367.         $result = array();
  368.         foreach ($r as $i => $f{
  369.             $result[$files[$i];
  370.         }
  371.     
  372.         return $result;
  373.     }
  374. }
  375.  
  376. ?>

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