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 ...
随机推荐
- CF 439C Devu and Partitioning of the Array
题目链接: 传送门 Devu and Partitioning of the Array time limit per test:1 second memory limit per test: ...
- UVa 10720 - Graph Construction(Havel-Hakimi定理)
题目链接: 传送门 Graph Construction Time Limit: 3000MS Memory Limit: 65536K Description Graph is a coll ...
- HTML之:让网页中的<a>标签属性统一设置-如‘新窗口打开’
在开发过程中,我们往往想在页面中,给<a>设置一个统一的默认格式,例如我们想让链接:“在新窗口打开”,我们就可以使用<base>标签 在网页中添加这段代码: <head& ...
- CBOW Model Formula Deduction
Paper Reference: word2vec Parameter Learning Explained 1. One-word context Model In our setting, the ...
- Apache配置HTTPS功能
apache配置https 一.yum 安装openssl和openssl-devel,httpd-devel 二.生成证书(也可以从公司的证书颁发机构获取): #建立服务器密钥 openssl ge ...
- 深入JVM-Class装载系统
一.Class文件的装载过程 Class类型通常以文件的形式存在(当然,任何二进制流都可以是Class类型),只有被Java虚拟机装载的Class类型才能在程序中使用.系统状态Class类型可以分为加 ...
- wpf 保存控件中的内容为图片格式
黄色的是wpf控件的名称! //保存到特定路径 FileStream fs = new FileStream(@"C:\image.png", FileMod ...
- 用sql 语句给字段添加描述
用sql 语句给字段添加描述 IF not exists (SELECT * FROM ::fn_listextendedproperty (NULL, 'user', 'dbo', 'table', ...
- 自然语言0_nltk中文使用和学习资料汇总
http://blog.csdn.net/huyoo/article/details/12188573 官方数据 http://www.nltk.org/book/ Natural Language ...
- 20145212 实验四《Andoid开发基础》
20145212 实验四<Andoid开发基础> 实验内容 安装Android Studio 运行安卓AVD模拟器 使用Android运行出模拟手机并显示自己的学号 实验过程 一.安装An ...