Source for file FileUtil.php

Documentation is available at FileUtil.php

  1. <?php
  2. /**
  3.  * Teeple2 - PHP5 Web Application Framework inspired by Seasar2
  4.  *
  5.  * PHP versions 5
  6.  *
  7.  * LICENSE: This source file is subject to version 3.0 of the PHP license
  8.  * that is available through the world-wide-web at the following URI:
  9.  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  10.  * the PHP License and are unable to obtain it through the web, please
  11.  * send a note to license@php.net so we can mail you a copy immediately.
  12.  *
  13.  * @package     teeple
  14.  * @author      Mitsutaka Sato <miztaka@gmail.com>
  15.  * @license     http://www.php.net/license/3_0.txt  PHP License 3.0
  16.  */
  17.  
  18. /**
  19.  * ディレクトリ・ファイル操作クラス
  20.  *
  21.  * @package     teeple
  22.  */
  23. {
  24.     /**
  25.      * include_pathからファイルを検索し、絶対パスの形で返す
  26.      * 見つからなかった場合はfalseを返す
  27.      * 
  28.      * @author  Hawk <scholar@hawklab.jp>
  29.      * @since   3.2.0
  30.      * @return  String or false
  31.      */
  32.     function findIncludableFile($path)
  33.     {
  34.         if (!is_array($include_paths explode(PATH_SEPARATORget_include_path()))) {
  35.             return realpath($path);
  36.         }
  37.         
  38.         foreach ($include_paths as $include_path{
  39.             if (($realpath realpath($include_path DIRECTORY_SEPARATOR $path)) !== false{
  40.                 return $realpath;
  41.             }
  42.         }
  43.         return false;
  44.     }
  45.  
  46.     /**
  47.      * ファイルを読み込む
  48.      *
  49.      * ファイルを読み込んで一つの文字列として返す。
  50.      *
  51.      * @param string $fileName  ファイル名
  52.      * @return string  ファイルの内容
  53.      * @access public
  54.      */
  55.     function read($fileName)
  56.     {
  57.         $buf '';
  58.         if (file_exists($fileName)) {
  59.             if (function_exists('file_get_contents')) {
  60.                 $buf file_get_contents($fileName);
  61.             else {
  62.                 $fh fopen($fileName"rb");
  63.                 $buf fread($fhfilesize($fileName));
  64.                 fclose($fh)
  65.             }
  66.         }
  67.         return $buf;
  68.     }
  69.  
  70.     /**
  71.      * ファイルに追記する
  72.      *
  73.      * 指定した内容をファイルに追記で書き込む。
  74.      * 途中のフォルダが存在しない場合は自動的に作成する。
  75.      *
  76.      * @param string $fileName  ファイル名
  77.      * @param string $buf  書き込む内容
  78.      * @return boolean 
  79.      * @access public
  80.      */
  81.     function append($fileName$buf)
  82.     {
  83.         return self::write($fileName$buf"ab");
  84.     }
  85.  
  86.     /**
  87.      * ファイルに書き込む
  88.      *
  89.      * 指定した内容をファイルに上書きで書き込む。
  90.      * 途中のフォルダが存在しない場合は自動的に作成する。
  91.      *
  92.      * @param string $fileName  ファイル名
  93.      * @param string $buf  書き込む内容
  94.      * @param string $mode  書き込みモード
  95.      * @return boolean 
  96.      * @access public
  97.      */
  98.     function write($fileName$buf$mode "wb")
  99.     {
  100.         self::makeDir(dirname($fileName));
  101.         
  102.         if (!($fh fopen($fileName$mode))) {
  103.             return false;
  104.         }
  105.         if (!fwrite($fh$buf)) {
  106.             return false;
  107.         }
  108.         if (!fclose($fh)) {
  109.             return false;
  110.         }
  111.         return true;
  112.     }
  113.  
  114.     /**
  115.      * ディレクトリを作成する。
  116.      * 
  117.      * 複数階層のディレクトリを指定した場合に、
  118.      * 途中のディレクトリも自動的に作成する。
  119.      * 
  120.      * @param mixed $dirNames  作成するディレクトリ名、またはその配列
  121.      * @access public
  122.      */
  123.     function makeDir($dirNames)
  124.     {
  125.         if (!is_array($dirNames)) {
  126.             $dirNames array($dirNames);
  127.         }
  128.         
  129.         foreach($dirNames as $dir){
  130.             self::_makeDir($dir);
  131.         }
  132.     }
  133.  
  134.     /**
  135.      * ディレクトリを削除する
  136.      *
  137.      * 指定したディレクトリより下のディレクトリ・ファイルも
  138.      * 自動的に削除する。
  139.      *
  140.      * @param mixed $dirNames  削除するディレクトリ名、またはその配列
  141.      * @access public
  142.      */
  143.     function removeDir($dirNames)
  144.     {
  145.         if (!is_array($dirNames)) {
  146.             $dirNames array($dirNames);
  147.         }
  148.         
  149.         foreach($dirNames as $dir){
  150.             self::_removeDir($dir);
  151.         }
  152.     }
  153.  
  154.     /**
  155.      * ディレクトリとファイルのリストを取得する
  156.      *
  157.      * 返値は配列で、最初がディレクトリの配列、次にファイルの配列となる。
  158.      * list($dirs, $files) = self::ls($dirname);
  159.      *
  160.      * @param string $dirName  ディレクトリ名
  161.      * @return array  ディレクトリとファイルの配列
  162.      * @access public
  163.      */
  164.     function ls($dirName)
  165.     {
  166.         if (!$dh @opendir($dirName)) {
  167.             return false;
  168.         }
  169.  
  170.         $dirs $files array();
  171.         while (($file readdir($dh)) !== false{
  172.             if (preg_match("/^[.]{1,2}$/"$file)) {
  173.                 continue;
  174.             }
  175.             $fullPath self::_addTail($dirNameDIRECTORY_SEPARATOR).$file;
  176.             if (is_dir($fullPath)) {
  177.                 $dirs[$fullPath;
  178.             else {
  179.                 $files[$fullPath;
  180.             }
  181.         }
  182.         closedir($dh);
  183.         return array($dirs$files);
  184.     }
  185.  
  186.     /**
  187.      * 指定したディレクトリのファイルリストを取得する
  188.      *
  189.      * @param string $dirName  ディレクトリ名
  190.      * @param string [$regex]  取得するファイル名の正規表現
  191.      * @return array  ファイルの配列
  192.      * @access public
  193.      */
  194.     function find($dirName$regex "")
  195.     {
  196.         $data self::ls($dirName);
  197.         if (!is_array($data)) {
  198.             return array();
  199.         }
  200.         list($dirs$files$data;
  201.  
  202.         if ($regex == ""{
  203.             return $files;
  204.         }
  205.         $found array();
  206.         foreach ($files as $file{
  207.             if (preg_match($regexbasename($file))) {
  208.                 $found[$file;
  209.             }
  210.         }
  211.         return $found;
  212.     }
  213.  
  214.     /**
  215.      * 指定したディレクトリのファイルリストを取得する
  216.      * 
  217.      * サブディレクトリがある場合は再帰的に取得する
  218.      *
  219.      * @param string $dirName  ディレクトリ名
  220.      * @param string [$regex]  取得するファイル名の正規表現
  221.      * @return array  ファイルの配列
  222.      * @access public
  223.      */
  224.     function findRecursive($dirName$regex "")
  225.     {
  226.         $data self::ls($dirName);
  227.         if (!is_array($data)) {
  228.             return array();
  229.         }
  230.         list($dirs$files$data;
  231.         
  232.         $found self::find($dirName$regex);
  233.  
  234.         foreach ($dirs as $dir{
  235.             $found array_merge($foundself::findRecursive($dir$regex));
  236.         }
  237.  
  238.         return $found;
  239.     }
  240.  
  241.     /**
  242.      * ディレクトリを作成する。
  243.      *
  244.      * 親ディレクトリがなければ自動的に作成する。
  245.      *
  246.      * @param string $dirName  ディレクトリ名
  247.      * @access private
  248.      */
  249.     function _makeDir($dirName)
  250.     {
  251.         $dirstack array();
  252.         while (!@is_dir($dirName&& $dirName != DIRECTORY_SEPARATOR{
  253.  
  254.             array_unshift($dirstack$dirName);
  255.             $dirName dirname($dirName);
  256.         }
  257.         while ($newdir array_shift($dirstack)) {
  258.             mkdir($newdir);
  259.         }
  260.     }
  261.  
  262.     /**
  263.      * ディレクトリを削除する
  264.      *
  265.      * サブディレクトリがあれば自動的に削除する。
  266.      * 指定したディレクトリ以下のファイルも自動的に削除する。
  267.      *
  268.      * @param string $dirName  ディレクトリ名
  269.      * @access private
  270.      */
  271.     function _removeDir($dirName)
  272.     {
  273.         $data self::ls($dirName);
  274.         if (!is_array($data)) {
  275.             return;
  276.         }
  277.         list($dirs$files$data;
  278.         
  279.         if (is_dir($dirName)) {
  280.             array_unshift($dirs$dirName);
  281.         }
  282.  
  283.         foreach($files as $file){
  284.             if (file_exists($file)) {
  285.                 unlink($file);
  286.             }
  287.         }
  288.  
  289.         foreach(array_reverse($dirsas $dir){
  290.             if (file_exists($dir)) {
  291.                 rmdir($dir);
  292.             }
  293.         }
  294.     }
  295.  
  296.     /**
  297.      * 文字列の末尾に文字列を追加する
  298.      *
  299.      * 文字列の後ろが追加する文字列と同じ場合は
  300.      * 何もしません。
  301.      * 
  302.      * @param string $target  追加対象の文字列
  303.      * @param string $add  追加する文字列
  304.      * @return string 変換後の文字列
  305.      * @access private
  306.      */
  307.     function _addTail($target$add)
  308.     {
  309.         $regex preg_quote($add);
  310.         if (!preg_match("|.*{$regex}$|"$target)) {
  311.             $target $target.$add;
  312.         }
  313.         return $target;
  314.     }
  315. }
  316. ?>

Documentation generated on Mon, 26 Apr 2010 08:59:47 +0900 by phpDocumentor 1.4.3