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 ...
随机推荐
- BZOJ3436 小K的农场
Description 背景 小K是个特么喜欢玩MC的孩纸... 描述 小K在MC里面建立很多很多的农场,总共n个,以至于他自己都忘记了每个农场中种植作物的具体数量了,他只记得 一些含糊的信息(共m个 ...
- JSP生命周期
1.编译阶段:servlet容器编译servlet源文件,生成servlet类.观察一个JSP页面在第一次访问的时候会由servlet容器会生成.java文件,最终编译成.class字节码文件,如果打 ...
- 禁止apache显示目录索引的常见方法(apache禁止列目录)
禁止Apache显示目录索引,禁止Apache显示目录结构列表,禁止Apache浏览目录,这是网上提问比较多的,其实都是一个意思.下面说下禁止禁止Apache显示目录索引的常见的3种方法. 要实现禁止 ...
- php面向对象中static静态属性和静态方法的调用
这篇文章主要介绍了php面向对象中static静态属性和静态方法的调用,实例分析了static静态属性和静态方法的原理与调用技巧,需要的朋友可以参考下 本文实例讲述了php中static静态属性和静态 ...
- 远程连接RabbitMQ失败
远程连接RabbitMQ失败 为了避免污染宿主系统环境,于是在虚拟机中搭建了一个linux环境并且按照了rabbitmq-server.然后在远程连接的时候一直连接失败. 官网上面给的例子都是在本地使 ...
- C++ 第一次课堂作业
GitHub链接: 传送门 题目描述 Create a program that asks for the radius of a circle and prints the area of that ...
- Spring--PropertyPlaceholderConfigurer
1. PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是 BeanFactoryPostProcessor接口的一个实现.PropertyPlaceho ...
- linux下memcache的运用,和php结合小案例。
由于是采用脚本安装的memache,所以软件的依赖关系我就不操心了,脚本已经帮我装好了和php的关联关系,实在是很省心.后续如果有需要,我会针对windows和linux各写一个安装和配置的说明,一来 ...
- UVA11178 Morley's Theorem(基础模板)
题目链接 题意:给出A,B, C点坐标求D,E,F坐标,其中每个角都被均等分成三份 求出 ABC的角a, 由 BC 逆时针旋转 a/3 得到BD,然后 求出 ACB 的角a2, 然后 由 BC顺时 ...
- css010 css的transform transition和animation
css010 css的transform transition和animation 看着没有一个能想起他们是干什么的.. 1. Transform Transform(变形) r ...