PHP文件操作的经典案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | <?php /* 遍历目录函数,只读取目录的最外层的内容 */ function readDirectory( $path ){ $handle = opendir( $path ); while (( $item = readdir( $handle ))!== false){ //.和..这2个特殊的目录 if ( $item != "." && $item != ".." ){ if ( is_file ( $path . "/" . $item )){ $arr [ "file" ][]= $item ; } if ( is_dir ( $path . "/" . $item )){ $arr [ "dir" ][]= $item ; } } } closedir ( $handle ); return $arr ; } /* 获取文件夹大小 */ function dirSize( $path ){ $sum =0; global $sum ; $handle =opendir( $path ); while (( $item =readdir( $handle ))!==false){ if ( $item != "." && $item != ".." ){ if ( is_file ( $path . "/" . $item )){ $sum += filesize ( $path . "/" . $item ); } if ( is_dir ( $path . "/" . $item )){ $func = __FUNCTION__ ; $func ( $path . "/" . $item ); } } } closedir ( $handle ); return $sum ; } /* 检测文件夹名称是否合法 */ function checkFilename( $filename ){ $pattern = "/[\/,\*,<>,\?\|]/" ; if (preg_match( $pattern , $filename )){ return false; } else { return true; } } /* 创建文件夹 */ function createFolder( $dirname ){ //检测文件夹名称是否合法 if (checkFilename( basename ( $dirname ))){ //当前目录下是否存在同名文件夹名称 if (! file_exists ( $dirname )){ if ( mkdir ( $dirname ,0777,true)){ $mes = "文件夹创建成功" ; } else { $mes = "文件夹创建失败" ; } } else { $mes = "存在相同文件夹名称" ; } } else { $mes = "非法文件夹名称" ; } return $mes ; } /* 重命名文件夹 */ function renameFolder( $oldname , $newname ){ //检测文件夹名称是否合法 if (checkFilename( basename ( $newname ))){ //检测当前目录下是否存在同名文件夹名称 if (! file_exists ( $newname )){ if (rename( $oldname , $newname )){ $mes = "重命名成功" ; } else { $mes = "重命名失败" ; } } else { $mes = "存在同名文件夹" ; } } else { $mes = "非法文件夹名称" ; } return $mes ; } /* 复制文件夹 */ function copyFolder( $src , $dst ){ if (! file_exists ( $dst )){ mkdir ( $dst ,0777,true); } $handle =opendir( $src ); while (( $item =readdir( $handle ))!==false){ if ( $item != "." && $item != ".." ){ if ( is_file ( $src . "/" . $item )){ copy ( $src . "/" . $item , $dst . "/" . $item ); } if ( is_dir ( $src . "/" . $item )){ $func = __FUNCTION__ ; $func ( $src . "/" . $item , $dst . "/" . $item ); } } } closedir ( $handle ); return "复制成功" ; } /* 剪切文件夹 */ function cutFolder( $src , $dst ){ if ( file_exists ( $dst )){ if ( is_dir ( $dst )){ if (! file_exists ( $dst . "/" . basename ( $src ))){ if (rename( $src , $dst . "/" . basename ( $src ))){ $mes = "剪切成功" ; } else { $mes = "剪切失败" ; } } else { $mes = "存在同名文件夹" ; } } } else { $mes = "目标文件夹不存在" ; } return $mes ; } /* 删除文件夹 */ function delFolder( $path ){ $handle =opendir( $path ); //打开目录,获取句柄 while (( $item =readdir( $handle ))!==false){ //读取句柄资源 if ( $item != "." && $item != ".." ){ if ( is_file ( $path . "/" . $item )){ unlink( $path . "/" . $item ); } if ( is_dir ( $path . "/" . $item )){ $func = __FUNCTION__ ; //自身函数名称 $func ( $path . "/" . $item ); } } } closedir ( $handle ); rmdir ( $path ); //删除目录,目录必须为空才能删除 return "文件夹删除成功" ; } ?> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!