1、检测目录是否存在
| | function check_dir($path, $create = false) { | | if (is_dir($path)) { | | return true; | | } | | return false; | | } |
|
2、创建目录
| | function create_dir($path) { | | if (! file_exists($path)) { | | if (mkdir($path, 0777, true)) { | | return true; | | } | | } | | return false; | | } |
|
3、检查文件是否存在
| | function check_file($path, $create = false, $content = null) { | | if (file_exists($path)) { | | return true; | | } | | return false; | | } |
|
4、创建文件
| | function create_file($path, $content = null, $over = false) { | | if (file_exists($path) && ! $over) { | | return false; | | } elseif (file_exists($path)) { | | @unlink($path); | | } | | check_dir(dirname($path), true); | | $handle = fopen($path, 'w') or error('创建文件失败,请检查目录权限!'); | | fwrite($handle, $content); | | return fclose($handle); | | } |
|
5、目录文件夹列表
| | function dir_list($path) { | | $list = array(); | | if (! is_dir($path) || ! $filename = scandir($path)) { | | return $list; | | } | | $files = count($filename); | | for ($i = 0; $i < $files; $i ++) { | | $dir = $path . '/' . $filename[$i]; | | if (is_dir($dir) && $filename[$i] != '.' && $filename[$i] != '..') { | | $list[] = $filename[$i]; | | } | | } | | return $list; | | } |
|
6、目录文件列表
| | function file_list($path) { | | $list = array(); | | if (! is_dir($path) || ! $filename = scandir($path)) { | | return $list; | | } | | $files = count($filename); | | for ($i = 0; $i < $files; $i ++) { | | $dir = $path . '/' . $filename[$i]; | | if (is_file($dir)) { | | $list[] = $filename[$i]; | | } | | } | | return $list; | | } |
|
7、目录下文件及文件夹列表
| | function path_list($path) { | | $list = array(); | | if (! is_dir($path) || ! $filename = scandir($path)) { | | return $list; | | } | | $files = count($filename); | | for ($i = 0; $i < $files; $i ++) { | | $dir = $path . '/' . $filename[$i]; | | if (is_file($dir) || (is_dir($dir) && $filename[$i] != '.' && $filename[$i] != '..')) { | | $list[] = $filename[$i]; | | } | | } | | return $list; | | } |
|
8、删除目录及目录下所有文件或删除指定文件
| | | | | | | | | | | | | | function path_delete($path, $delDir = false, $exFile = array()) { | | $result = true; | | if (! file_exists($path)) { | | return $result; | | } | | if (is_dir($path)) { | | if (! ! $dirs = scandir($path)) { | | foreach ($dirs as $value) { | | if ($value != "." && $value != ".." && ! in_array($value, $exFile)) { | | $dir = $path . '/' . $value; | | $result = is_dir($dir) ? path_delete($dir, $delDir, $exFile) : unlink($dir); | | } | | } | | if ($result && $delDir) { | | return rmdir($path); | | } else { | | return $result; | | } | | } else { | | return false; | | } | | } else { | | return unlink($path); | | } | | } |
|
9、拷贝文件夹
| | function dir_copy($src, $des, $son = 1) { | | if (! is_dir($src)) { | | return false; | | } | | if (! is_dir($des)) { | | create_dir($des); | | } | | $handle = dir($src); | | while (! ! $path = $handle->read()) { | | if (($path != ".") && ($path != "..")) { | | if (is_dir($src . "/" . $path)) { | | if ($son) | | dir_copy($src . "/" . $path, $des . "/" . $path, $son); | | } else { | | copy($src . "/" . $path, $des . "/" . $path); | | } | | } | | } | | return true; | | } |
|
路过支持一下