1. 程式人生 > >php 的檔案操作類

php 的檔案操作類

<?php
header('Content-type:text/html;charset=utf8');

Class FILE
{
    private static $path;
    private static $files = [];
    private static $dirs = [];

    private function __construct($path)
    {
        try {
            if (is_dir($path)) {
                self::$path = strtr($path, ['\\' => '/']);
            }
        } 
catch (\Exception $e) { echo $e->getMessage(); } } private function runFiles($path) { $arr = ['files' => [], 'dirs' => [], 'all' => []]; $target = array_diff(scandir($path), ['.', '..']); array_walk($target, function ($val, $key) use
(&$arr, $path) { $subTarget = "{$path}/{$val}"; if (is_file($subTarget)) { array_push($arr['files'], "{$path}/" . $val); } else if (is_dir($subTarget)) { array_push($arr['dirs'], "{$path}/" . $val); $arr = array_merge_recursive
($arr, $this->runFiles($subTarget)); } }); return $arr; } /**新建資料夾,如果目標資料夾不存在的情況下 * @param $target * @return mixed */ private static function createFile($target) { if (!is_dir($target)) { mkdir($target, 0777, true); } return $target; } /**判斷是否是空的資料夾 * @param $dir * @return bool */ private static function isEmptyDir($dir) { $arr = array_diff(scandir($dir), ['.', '..']); return count($arr) == 0 ? true : false; } /**初始化 * @param $path * @return FILE */ public static function init($path) { $cls = new self($path); $all = $cls->runFiles(self::$path); self::$files = $all['files']; self::$dirs = $all['dirs']; return $cls; } /**處理檔案如複製或移動 * @param $target * @param $mode * @param $extension * @return int */ private function dealFile($target, $mode, $extension) { $target = self::createFile($target); $result = 0; array_walk(self::$files, function ($val) use ($target, $extension, $mode, &$result) { $info = pathinfo($val); if (!$extension || ($extension && strcasecmp($info['extension'], $extension) == 0)) { $res = strcasecmp($mode, 'move') == 0 ? rename($val, $target . '/' . $info['basename']) : copy($val, $target . '/' . $info['basename']); if ($res) { $result++; } } }); return $result; } /**獲取真實的檔案路徑 * @return array */ public function getRawFiles() { return self::$files; } /**獲取真實的資料夾路徑 * @return array */ public function getRawDirs() { return self::$dirs; } /**獲取全部的檔名 * @return array */ public function getFiles() { $arr = []; array_walk(self::$files, function ($val) use (&$arr) { array_push($arr, basename($val)); }); return $arr; } /**獲取所有的資料夾 * @return array */ public function getDirs() { $arr = []; array_walk(self::$dirs, function ($val) use (&$arr) { array_push($arr, basename($val)); }); return $arr; } /**獲取樹形結構圖,注意這邊的引用傳值 * @return array */ public function getTree() { $all = array_merge(self::$dirs, self::$files); $tree = []; $diff = explode('/', self::$path); if ($all) { array_walk($all, function ($val) use ($diff, &$tree) { $temp_arr = explode('/', $val); if (is_file($val)) { $file = end($temp_arr); array_push($diff, $file); } $temp_arr = array_diff($temp_arr, $diff); $parent =& $tree; foreach ($temp_arr as $k => $v) { if (!$parent[$v]) { $parent[$v] = []; } $parent =& $parent[$v]; } if (is_file($val)) { array_push($parent, $file); } }); } return $tree; } /**展示資料夾的資訊 * @return array */ public function getInfo() { $files = self::$files; $dirs = self::$dirs; $size = 0; array_walk($files, function ($val) use (&$size) { $size += filesize($val); }); return [ 'size' => $size, 'dirs' => count($dirs), 'files' => count($files) ]; } /**進行檔案拷貝 * @param $target * @param null $type * @return int */ public function copyFiles($target, $type = null) { return $this->dealFile($target, 'copy', $type); } /**複製所有的空資料夾 * @param $target * @return int */ public function copyDirs($target) { $dirs = self::$dirs; $target = strtr(trim($target), ['\\' => '/']); $target_arr = explode('/', $target); if (end($target_arr) == '') { array_pop($target_arr); } $diff = explode('/', self::$path); $count = 0; array_walk($dirs, function ($val) use (&$count, $target_arr, $diff) { $temp_arr = array_diff(explode('/', $val), $diff); $new_path = implode('/', $target_arr) . '/' . implode('/', $temp_arr); if (mkdir($new_path, 0777, true)) { $count++; } }); return $count; } /**檔案的剪下 * @param $target * @param null $type * @return int */ public function moveFiles($target, $type = null) { return $this->dealFile($target, 'move', $type); } /**剪下所有的資料夾以及檔案 * @param $target * @return array */ public function moveAll($target) { $dirs = $this->copyDirs($target); $files = self::$files; $target_arr = explode('/', $target); if (end($target_arr) == '') { array_pop($target_arr); } $diff = explode('/', self::$path); $count = 0; array_walk($files, function ($val) use (&$count, $target_arr, $diff) { $temp_arr = array_diff(explode('/', $val), $diff); $new_path = implode('/', $target_arr) . '/' . implode('/', $temp_arr); if (rename($val, $new_path)) { $count++; } }); $this->removeAll(); return [ 'files' => $count, 'dirs' => $dirs ]; } /**刪除指定目錄下的所有檔案 * @return int */ public function removeFiles() { $count = 0; array_walk(self::$files, function ($val) use (&$count) { if (unlink($val)) { $count++; } }); return $count; } /**進行刪除資料夾所有內容的操作 * @return bool */ public function removeAll() { $dirs = self::$dirs; //進行資料夾排序 uasort($dirs, function ($m, $n) { return strlen($m) > strlen($n) ? -1 : 1; }); //刪除所有檔案 $this->removeFiles(); array_walk($dirs, function ($val) { rmdir($val); }); return self::isEmptyDir(self::$path); } } $path = 'd:/filetest'; $target = 'd:/yftest'; //所有介面展示 //獲取所有的檔名稱,含完整路徑 FILE::init($path)->getRawFiles(); //獲取所有的檔名稱,不含路徑 FILE::init($path)->getFiles(); //獲取所有的資料夾名稱,含完整路徑 FILE::init($path)->getRawDirs(); //獲取所有的資料夾名稱,不含路徑 FILE::init($path)->getDirs(); //獲取目標資料夾$path的樹形結構圖 FILE::init($path)->getTree(); //獲取目標資料夾$path的資訊 FILE::init($path)->getInfo(); //把$path下的所有檔案複製到$target目錄下,如果有指定型別的情況下,那麼只複製指定型別的檔案 FILE::init($path)->copyFiles($target, 'php'); //把$path下的所有資料夾複製到$target目錄下,並且按$path的層級擺放 FILE::init($path)->copyDirs($target); //把$path下的所有檔案剪下到$taret目錄下,如果有指定型別的情況下,那麼只移動指定型別的檔案 FILE::init($path)->moveFiles($target, 'php'); //把$path下的所有檔案及資料夾移動到$target目錄下,並且不改變原有的層級結構 FILE::init($path)->moveAll($target); //刪除指定資料夾下的所有檔案,不含資料夾 FILE::init($path)->removeFiles(); //刪除指定路徑下的所有內容含檔案,資料夾 FILE::init($path)->removeAll(); ?>