<?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();
?>

php 的文件操作类的更多相关文章

  1. [C#] 常用工具类——文件操作类

    /// <para> FilesUpload:工具方法:ASP.NET上传文件的方法</para> /// <para> FileExists:返回文件是否存在&l ...

  2. 文件操作类CFile

    CFile file; CString str1= L"写入文件成功!"; wchar_t *str2; if (!file.Open(L"Hello.txt" ...

  3. asp.net文件操作类

    /** 文件操作类 **/ #region 引用命名空间 using System; using System.Collections.Generic; using System.Text; usin ...

  4. android 文件操作类简易总结

    android 文件操作类(参考链接) http://www.cnblogs.com/menlsh/archive/2013/04/02/2997084.html package com.androi ...

  5. Ini文件操作类

    /// <summary> /// Ini文件操作类 /// </summary> public class Ini { // 声明INI文件的写操作函数 WritePriva ...

  6. java csv 文件 操作类

    一个CSV文件操作类,功能比较齐全: package tool; import java.io.BufferedReader; import java.io.BufferedWriter; impor ...

  7. Qt5:Qt文件操作类 QFile

    在QT中,操作文件一般不使用C++提供的文件操作类 , 因为操作文件的时候,要用到C++提供的 string 类,而在QT中使用的是Qt自己实现的一个string类 QString .在Qt中使用C+ ...

  8. C# 文件操作类大全

      C# 文件操作类大全 时间:2015-01-31 16:04:20      阅读:1724      评论:0      收藏:0      [点我收藏+] 标签: 1.创建文件夹 //usin ...

  9. Java文件操作类效率对比

    前言 众所周知,Java中有多种针对文件的操作类,以面向字节流和字符流可分为两大类,这里以写入为例: 面向字节流的:FileOutputStream 和 BufferedOutputStream 面向 ...

  10. JAVA文件操作类和文件夹的操作代码示例

    JAVA文件操作类和文件夹的操作代码实例,包括读取文本文件内容, 新建目录,多级目录创建,新建文件,有编码方式的文件创建, 删除文件,删除文件夹,删除指定文件夹下所有文件, 复制单个文件,复制整个文件 ...

随机推荐

  1. [转帖]CPU Cache 机制以及 Cache miss

    CPU Cache 机制以及 Cache miss https://www.cnblogs.com/jokerjason/p/10711022.html CPU体系结构之cache小结 1.What ...

  2. kettle查询

    >流查询: 1.转换设计 2.主数据 3.查询数据 4.流查询 5.数据预览 查询中有重复数据默认获取最后一条:查询数据中有重复数据,默认获取到了最后一条数据. 主数据中无匹配数据则在结果集中返 ...

  3. 转 spring注解式参数校验

    转自: https://blog.csdn.net/jinzhencs/article/details/51682830 转自: https://blog.csdn.net/zalan01408980 ...

  4. Lodop的TABLE中format格式化的使用

    LODOP中的ADD_PRINT_TABLE支持很多函数和计算方法,可以用tdata对table表格里额数据进行计算,用format对结果进行格式化.这个format只能和tdata搭配使用,不能单独 ...

  5. LODOP不同打印机出现偏移问题

    方法简单描述:1.精确套打,设置以纸张边缘为基点,可避免不同可打区域不同带了的影响.2.不同客户端打印机位置差异,可通过打印维护调整,结果在客户端本地.或调整打印机初始位置(本人使用的金税盘的开票软件 ...

  6. HBase轻松入门之HBase架构图解析

    2018-12-13 2018-12-20 本篇文章旨在针对初学者以我本人现阶段所掌握的知识就HBase的架构图中各模块作一个概念科普.不对文章内容的“绝对.完全正确性”负责. 1.开胃小菜 关于HB ...

  7. edusoho -A5: ApiBundle UML

    edusoho -A5:  ApiBundle UML

  8. rt-thread 之组件与设备初始化配置

    @2019-03-08 [小记] rt-thread 初始化配置有两个分支: 第一,板级设备初始化 rt_components_board_init() 第二,内核组件初始化 rt_component ...

  9. jenkins在windows及linux环境下安装

    下载 下载地址: https://jenkins.io/download/ 下载windows和linux通用的war包 jenkins在windows下安装 前提:已经安装jdk.tomcat 将w ...

  10. 洛谷P5289 皮配

    解:观察一波部分分. 首先小数据直接暴力4n,然后考虑背包.设f[i][a][b][c]表示前i个学校中前三位导师分别有多少人,第四位导师可以直接推出来. 然后暴力枚举每一个人放在哪进行背包. 进一步 ...