<?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. Python中数学函数

    1.不需要引入math模块的有: abs(),cmp(),max(),min(),pow(),round() 2.需要引入math模块的: 三角函数,及其他数学函数,fabs(), *需要特别注意: ...

  2. 父元素高度设置为min-height,子元素高度设置为100%,但实际上子元素高度你知道是多少吗?

    前言 给父元素一个min-height,子元素设置height:100%. 代码 <!DOCTYPE html> <html> <head> <title&g ...

  3. synchronized详解

    关于synchronized,本文从使用方法,底层原理和锁的升级优化这几个方面来介绍. 1.synchronized的使用: synchronized可以保证在同一时刻,只有一个线程可以操作共享变量, ...

  4. 记录下mainfest.json 原生标题的按钮监听

    首先在mainfest.json中 plus下添加以下代码 "launchwebview": {"titleNView": {"backgroundc ...

  5. vue 自定义指令的使用案例

    参考资料: 1. vue 自定义指令: 2. vue 自定义指令实现 v-loading: v-loading,是 element-ui 组件库中的一个用于数据加载过程中的过渡动画指令,项目中也很少需 ...

  6. 关于计时器的js函数

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...

  7. Codeforces Round #551 (Div. 2) 题解

    CF1153A 直接做啊,分类讨论即可 #include<iostream> #include<string.h> #include<string> #includ ...

  8. 如何确定Kafka的分区数、key和consumer线程数

    [原创]如何确定Kafka的分区数.key和consumer线程数   在Kafak中国社区的qq群中,这个问题被提及的比例是相当高的,这也是Kafka用户最常碰到的问题之一.本文结合Kafka源码试 ...

  9. 【转】 如何重写hashCode()和equals()方法

    转自:http://blog.csdn.net/neosmith/article/details/17068365 hashCode()和equals()方法可以说是Java完全面向对象的一大特色.它 ...

  10. 连接SQL Server数据库

    SqlConnection来连接数据库,注意数据库目标的格式. using System.Data.SqlClient;//载入数据库命名空间 namespace WindowsFormsApplic ...