php简单实用的操作文件工具类(创建、移动、复制、删除)
- function recurse_copy($src,$dst) { // 原目录,复制到的目录
- $dir = opendir($src);
- @mkdir($dst);
- while(false !== ( $file = readdir($dir)) ) {
- if (( $file != '.' ) && ( $file != '..' )) {
- if ( is_dir($src . '/' . $file) ) {
- recurse_copy($src . '/' . $file,$dst . '/' . $file);
- }
- else {
- copy($src . '/' . $file,$dst . '/' . $file);
- }
- }
- }
- closedir($dir);
- }
- echo recurse_copy("原文件夹","目录文件夹");
还有更流弊的工具类:
- <?php
- /**
- * 操纵文件类
- *
- * 例子:
- * FileUtil::createDir('a/1/2/3'); 测试建立文件夹 建一个a/1/2/3文件夹
- * FileUtil::createFile('b/1/2/3'); 测试建立文件 在b/1/2/文件夹下面建一个3文件
- * FileUtil::createFile('b/1/2/3.exe'); 测试建立文件 在b/1/2/文件夹下面建一个3.exe文件
- * FileUtil::copyDir('b','d/e'); 测试复制文件夹 建立一个d/e文件夹,把b文件夹下的内容复制进去
- * FileUtil::copyFile('b/1/2/3.exe','b/b/3.exe'); 测试复制文件 建立一个b/b文件夹,并把b/1/2文件夹中的3.exe文件复制进去
- * FileUtil::moveDir('a/','b/c'); 测试移动文件夹 建立一个b/c文件夹,并把a文件夹下的内容移动进去,并删除a文件夹
- * FileUtil::moveFile('b/1/2/3.exe','b/d/3.exe'); 测试移动文件 建立一个b/d文件夹,并把b/1/2中的3.exe移动进去
- * FileUtil::unlinkFile('b/d/3.exe'); 测试删除文件 删除b/d/3.exe文件
- * FileUtil::unlinkDir('d'); 测试删除文件夹 删除d文件夹
- */
- class FileUtil {
- /**
- * 建立文件夹
- *
- * @param string $aimUrl
- * @return viod
- */
- function createDir($aimUrl) {
- $aimUrl = str_replace('', '/', $aimUrl);
- $aimDir = '';
- $arr = explode('/', $aimUrl);
- $result = true;
- foreach ($arr as $str) {
- $aimDir .= $str . '/';
- if (!file_exists($aimDir)) {
- $result = mkdir($aimDir);
- }
- }
- return $result;
- }
- /**
- * 建立文件
- *
- * @param string $aimUrl
- * @param boolean $overWrite 该参数控制是否覆盖原文件
- * @return boolean
- */
- function createFile($aimUrl, $overWrite = false) {
- if (file_exists($aimUrl) && $overWrite == false) {
- return false;
- } elseif (file_exists($aimUrl) && $overWrite == true) {
- FileUtil :: unlinkFile($aimUrl);
- }
- $aimDir = dirname($aimUrl);
- FileUtil :: createDir($aimDir);
- touch($aimUrl);
- return true;
- }
- /**
- * 移动文件夹
- *
- * @param string $oldDir
- * @param string $aimDir
- * @param boolean $overWrite 该参数控制是否覆盖原文件
- * @return boolean
- */
- function moveDir($oldDir, $aimDir, $overWrite = false) {
- $aimDir = str_replace('', '/', $aimDir);
- $aimDir = substr($aimDir, -1) == '/' ? $aimDir : $aimDir . '/';
- $oldDir = str_replace('', '/', $oldDir);
- $oldDir = substr($oldDir, -1) == '/' ? $oldDir : $oldDir . '/';
- if (!is_dir($oldDir)) {
- return false;
- }
- if (!file_exists($aimDir)) {
- FileUtil :: createDir($aimDir);
- }
- @ $dirHandle = opendir($oldDir);
- if (!$dirHandle) {
- return false;
- }
- while (false !== ($file = readdir($dirHandle))) {
- if ($file == '.' || $file == '..') {
- continue;
- }
- if (!is_dir($oldDir . $file)) {
- FileUtil :: moveFile($oldDir . $file, $aimDir . $file, $overWrite);
- } else {
- FileUtil :: moveDir($oldDir . $file, $aimDir . $file, $overWrite);
- }
- }
- closedir($dirHandle);
- return rmdir($oldDir);
- }
- /**
- * 移动文件
- *
- * @param string $fileUrl
- * @param string $aimUrl
- * @param boolean $overWrite 该参数控制是否覆盖原文件
- * @return boolean
- */
- function moveFile($fileUrl, $aimUrl, $overWrite = false) {
- if (!file_exists($fileUrl)) {
- return false;
- }
- if (file_exists($aimUrl) && $overWrite = false) {
- return false;
- } elseif (file_exists($aimUrl) && $overWrite = true) {
- FileUtil :: unlinkFile($aimUrl);
- }
- $aimDir = dirname($aimUrl);
- FileUtil :: createDir($aimDir);
- rename($fileUrl, $aimUrl);
- return true;
- }
- /**
- * 删除文件夹
- *
- * @param string $aimDir
- * @return boolean
- */
- function unlinkDir($aimDir) {
- $aimDir = str_replace('', '/', $aimDir);
- $aimDir = substr($aimDir, -1) == '/' ? $aimDir : $aimDir . '/';
- if (!is_dir($aimDir)) {
- return false;
- }
- $dirHandle = opendir($aimDir);
- while (false !== ($file = readdir($dirHandle))) {
- if ($file == '.' || $file == '..') {
- continue;
- }
- if (!is_dir($aimDir . $file)) {
- FileUtil :: unlinkFile($aimDir . $file);
- } else {
- FileUtil :: unlinkDir($aimDir . $file);
- }
- }
- closedir($dirHandle);
- return rmdir($aimDir);
- }
- /**
- * 删除文件
- *
- * @param string $aimUrl
- * @return boolean
- */
- function unlinkFile($aimUrl) {
- if (file_exists($aimUrl)) {
- unlink($aimUrl);
- return true;
- } else {
- return false;
- }
- }
- /**
- * 复制文件夹
- *
- * @param string $oldDir
- * @param string $aimDir
- * @param boolean $overWrite 该参数控制是否覆盖原文件
- * @return boolean
- */
- function copyDir($oldDir, $aimDir, $overWrite = false) {
- $aimDir = str_replace('', '/', $aimDir);
- $aimDir = substr($aimDir, -1) == '/' ? $aimDir : $aimDir . '/';
- $oldDir = str_replace('', '/', $oldDir);
- $oldDir = substr($oldDir, -1) == '/' ? $oldDir : $oldDir . '/';
- if (!is_dir($oldDir)) {
- return false;
- }
- if (!file_exists($aimDir)) {
- FileUtil :: createDir($aimDir);
- }
- $dirHandle = opendir($oldDir);
- while (false !== ($file = readdir($dirHandle))) {
- if ($file == '.' || $file == '..') {
- continue;
- }
- if (!is_dir($oldDir . $file)) {
- FileUtil :: copyFile($oldDir . $file, $aimDir . $file, $overWrite);
- } else {
- FileUtil :: copyDir($oldDir . $file, $aimDir . $file, $overWrite);
- }
- }
- return closedir($dirHandle);
- }
- /**
- * 复制文件
- *
- * @param string $fileUrl
- * @param string $aimUrl
- * @param boolean $overWrite 该参数控制是否覆盖原文件
- * @return boolean
- */
- function copyFile($fileUrl, $aimUrl, $overWrite = false) {
- if (!file_exists($fileUrl)) {
- return false;
- }
- if (file_exists($aimUrl) && $overWrite == false) {
- return false;
- } elseif (file_exists($aimUrl) && $overWrite == true) {
- FileUtil :: unlinkFile($aimUrl);
- }
- $aimDir = dirname($aimUrl);
- FileUtil :: createDir($aimDir);
- copy($fileUrl, $aimUrl);
- return true;
- }
- }
- ?>
php简单实用的操作文件工具类(创建、移动、复制、删除)的更多相关文章
- Properties文件工具类的使用--获取所有的键值、删除键、更新键等操作
有时候我们希望处理properties文件,properties文件是键值对的文件形式,我们可以借助Properties类操作. 工具类如下:(代码中日志采用了slf4j日志) package cn. ...
- java中文件操作的工具类
代码: package com.lky.pojo; import java.io.BufferedReader; import java.io.BufferedWriter; import java. ...
- 自己封装的poi操作Excel工具类
自己封装的poi操作Excel工具类 在上一篇文章<使用poi读写Excel>中分享了一下poi操作Excel的简单示例,这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完 ...
- 简单了解Spring中常用工具类_java - JAVA
文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 文件资源操作 Spring 定义了一个 org.springframework.core.io.Resource 接口, ...
- Redis操作List工具类封装,Java Redis List命令封装
Redis操作List工具类封装,Java Redis List命令封装 >>>>>>>>>>>>>>>> ...
- android操作ini工具类
package com.smarteye.common; import java.io.BufferedReader; import java.io.BufferedWriter; import ja ...
- 简单实用的PHP防注入类实例
这篇文章主要介绍了简单实用的PHP防注入类实例,以两个简单的防注入类为例介绍了PHP防注入的原理与技巧,对网站安全建设来说非常具有实用价值,需要的朋友可以参考下 本文实例讲述了简单实用的PHP防注 ...
- 自动扫描FTP文件工具类 ScanFtp.java
package com.util; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ja ...
- 读取Config文件工具类 PropertiesConfig.java
package com.util; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io ...
随机推荐
- Jenkins与Hudson的关系
Jenkins的前身是Hudson(Sun开发),2010年从Hudson分支出来. 由于Sun被Oracle收购,Oracle拥有Hudson的商标所有权.分支出来的Jenkins将继续走open ...
- asp.net接收ajax请求参数时为空的现象
如题,如果使用ajax请求asp.net后台时,如果使用jquery时,默认是添加了请求头,使后台能识别,并能通过Request对象进行获取. 但是如果你使用的是window.XMLHttpReque ...
- SPOJ GSS3 Can you answer these queries III
Time Limit: 330MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Description You are g ...
- grid列的值格式化
//列格式化 waf.defineCustomeClass("cellformatter.ratioFomatter", cellformatter.defaultFormatte ...
- 数据结构算法C语言实现(二十)--- 6.3.1遍历二叉树
一.简述 二叉树的遍历主要是先序.中序.后序及对应的递归和非递归算法,共3x2=6种,其中后序非递归在实现上稍复杂一些.二叉树的遍历是理解和学习递归及体会栈的工作原理的绝佳工具! 此外,非递归所用的栈 ...
- shell处理mysql增、删、改、查
引言 这几天做一个任务,比对两个数据表中的数据,昨天用PHP写了一个版本,但考虑到有的机器没有php或者php没有编译mysql扩展,就无法使用mysql系列的函数,脚本就无效了,今天写个sh ...
- Unity 单例写法
借鉴自:http://www.cnblogs.com/CodeCabin/p/unity_global_manager.html 实现复杂一些的全局控制,如切换游戏关卡等操作,更常用的方式是使用单例类 ...
- 如何保持自己 fork 的项目和原始项目同步
首先先通过 github 的 web 页面 fork 目标的项目 前提是自己已经设置好了git,并且配置了相应的权限 然后使用git clone命令在本地克隆自己 fork 的项目: git clon ...
- Groupby - collection processing
Groupby - collection processing Iterator and Iterable have most of the most useful methods when deal ...
- Gated Recurrent Unit (GRU)公式简介
update gate $z_t$: defines how much of the previous memory to keep around. \[z_t = \sigma ( W^z x_t+ ...