Home > Article > Backend Development > Summary of PHP directory operations
PHP directory operation (encapsulated directory operation function file attached)
Commonly used APIs for directory function libraries
$path='test'; var_dump(is_dir($path));//检测是否为目录 echo '<hr/>'; echo getcwd();//得到当前的工作目录 echo '<hr/>'; //得到磁盘总大小 echo disk_total_space('/'); echo '<hr/>'; //得到磁盘可用空间 echo disk_free_space('/');
Create and delete directories
Multi-level directories
//创建目录 //mkdir($path,$mode,$flag):创建目录 var_dump(mkdir('a')); //检测目录是否存在,不存在则创建 $path='king'; if(!file_exists($path)){ if(mkdir($path)){ echo '目录创建成功'; }else{ echo '目录创建失败'; } }else{ echo '目录已存在'; } //创建多级目录 b/c $path='b'.DIRECTORY_SEPARATOR.'c'; echo $path; var_dump(mkdir($path,755,true)); //删除目录 var_dump(rmdir('a')); var_dump(rmdir('b')); var_dump(rmdir('a'));
Read directories
$path='webuploader-master'; //打开指定目录 $handle=opendir($path); var_dump($handle);//resource(5) of type (stream) //遍历文件,获取类型 while(($item=readdir($handle))!==false){ echo $item,"\n"; echo filetype($path.DIRECTORY_SEPARATOR.$item),"\n";//获取文件类型 echo "\n"; } //遍历文件,获取类型2 while(($item=readdir($handle))!==false){ //去掉.和.. if($item!='.'&&$item!='..'){ //检测是否是文件 if(is_file($path.'/'.$item)){ echo '文件:',$item,"\n"; }else{ echo '目录:',$item,"\n"; } } } //rewinddir()重置遍历指针 rewinddir(); var_dump(readdir($handle));// . //关闭句柄 closedir($handle);
DirectoryIterator
Use of iterator
$dir=new DirectoryIterator(__DIR__); //print_r($dir); foreach($dir as $fileInfo){ if($fileInfo->getFilename()!='.'&&$fileInfo->getFilename()!='..'){ echo $fileInfo->getFilename(),"\n";//文件名 echo $fileInfo->getType(),"\n";//文件类型 } }
[Super practical directory-related function encapsulation]
dir.func.php
/** * 检测目录是否为空 * @method check_empty_dir * @param string $path 目录名 * @return boolean true|false */ function check_empty_dir(string $path){ //检测目录是否存在,存在则打开 if(!is_dir($path)){ return false; } //打开指定目录 $handle=opendir($path); //读取 while(($item=@readdir($handle))!==false){ //去掉.和..操作 if($item!='.'&&$item!='..'){ return false; } } //关闭句柄 closedir($handle); return true; } ------------------- /** * 读取目录下的所有文件 * @method read_directory * @param string $path 目录名称 * @return void 直接输出目录下的所有文件及子目录 */ function read_directory(string $path){ if(!is_dir($path)){ return false; } $handle=opendir($path); while(($item=@readdir($handle))!==false){ if($item!='.'&&$item!='..'){ $pathName=$path.DIRECTORY_SEPARATOR.$item; if(is_file($pathName)){ echo '文件:',$item,'<br/>'; }else{ echo '目录:',$item,'<br/>'; $func=__FUNCTION__; $func($pathName); } } } closedir($handle); } // read_directory('a'); ------------------- /** * 遍历目录下所有内容返回 * @method read_directory1 * @param string $path 目录名称 * @return mixed false|array */ function read_directory1(string $path){ if(!is_dir($path)){ return false; } $handle=opendir($path); while(($item=@readdir($handle))!==false){ if($item!='.'&&$item!='..'){ $pathName=$path.DIRECTORY_SEPARATOR.$item; if(is_file($pathName)){ $arr['file'][]=$pathName; }elseif(is_dir($pathName)){ $arr['dir'][]=$pathName; $func=__FUNCTION__; $func($pathName); } } } closedir($handle); return $arr; } // $res=read_directory1('a'); // print_r($res); ------------------- /** * 读取目录中的所有文件 * @method get_all_files * @param string $path 目录名称 * @return mixed false|array */ function get_all_files(string $path){ if(!is_dir($path)){ return false; } if($handle=opendir($path)){ $res=[]; while(($item=readdir($handle))!==false){ if($item!='.'&&$item!='..'){ $pathName=$path.DIRECTORY_SEPARATOR.$item; is_dir($pathName)?$res[$pathName]=get_all_files($pathName):$res[]=$pathName; } } closedir($handle); return $res; }else{ return false; } } // print_r(get_all_files('a')); ------------------- /** * 得到目录大小 * @method get_dir_size * @param string $path 目录名称 * @return mixed false|int */ function get_dir_size(string $path){ if(!is_dir($path)){ return false; } static $sum=0; $handle=opendir($path); while(($item=readdir($handle))!==false){ if($item!='.'&&$item!='..'){ $pathName=$path.DIRECTORY_SEPARATOR.$item; if(is_file($pathName)){ $sum+=filesize($pathName); }else{ $func=__FUNCTION__; $func($pathName); } } } closedir($handle); return $sum; } // echo get_dir_size('a'); ------------------- /** * 重命名目录 * @method rename_dir * @param string $oldName 原目录 * @param string $newName 新目录 * @return boolean true|false */ function rename_dir(string $oldName,string $newName){ //检测原文件是否存在,或者当前目录下存在同名目录 $dest=dirname($oldName).DIRECTORY_SEPARATOR.$newName; if(!is_dir($oldName)|| file_exists($dest)){ return false; } if(rename($oldName,$dest)){ return true; } return false; } //重命名 // var_dump(rename_dir('a','aaaa')); // var_dump(rename_dir('b','c')); ------------------- /** * 剪切目录 * @method cut_dir * @param string $src 原目录 * @param string $dst 新目录位置 * @return boolean true|false */ function cut_dir(string $src,string $dst){ //检测原目录是否存在,不存在返回false if(!is_dir($src)){ return false; } //检测目录路径是否存在,不存在则创建 if(!is_dir($dst)){ mkdir($dst,755,true); } //检测目录路径下是否存在同名目录 $dest=$dst.DIRECTORY_SEPARATOR.basename($src); if(is_dir($dest)){ return false; } //剪切 if(rename($src,$dest)){ return true; } return false; } // var_dump(cut_dir('d','uploads')); // var_dump(cut_dir('c','test1')); // var_dump(cut_dir('test1','test2')); ------------------- /** * 拷贝目录操作 * @method copy_dir * @param string $src 原目录 * @param string $dst 目标路径 * @return boolean true|false */ function copy_dir(string $src,string $dst){ //检测原目录是否存在 if(!is_dir($src)){ return false; } //检测目标目录是否存在,不存在则创建 if(!is_dir($dst)){ mkdir($dst,755,true); } //检测目标目录下是否存在同名文件 $dest=$dst.DIRECTORY_SEPARATOR.basename($src); if(is_dir($dest)){ return false; } $handle=opendir($src); while(($item=@readdir($handle))!==false){ if($item!='.'&&$item!='..'){ if(is_file($src.DIRECTORY_SEPARATOR.$item)){ copy($src.DIRECTORY_SEPARATOR.$item,$dst.DIRECTORY_SEPARATOR.$item); } if(is_dir($src.DIRECTORY_SEPARATOR.$item)){ $func=__FUNCTION__; $func($src.DIRECTORY_SEPARATOR.$item,$dst.DIRECTORY_SEPARATOR.$item); } } } closedir($handle); return true; } // var_dump(copy_dir('uploads','test2/uploads')); ------------------- /** * 删除非空目录 * @method del_dir * @param string $path 目录名称 * @return boolean true|false */ function del_dir(string $path){ //检测目录是否存在 if(!is_dir($path)){ return false; } $handle=opendir($path); while(($item=@readdir($handle))!==false){ if($item!='.'&&$item!='..'){ $pathName=$path.DIRECTORY_SEPARATOR.$item; if(is_file($pathName)){ @unlink($pathName); }else{ $func=__FUNCTION__; $func($pathName); } } } closedir($handle); rmdir($path); return true; } // var_dump(del_dir('test2'));
DIRECTORY_SEPARATOR
In Windows we habitually use "\" as the file separator, but on Linux the system does not recognize this identifier, so we have to This php built-in variable is introduced: DIRECTORY_SEPARATOR.
For example, if the development machine is Windows and there is an image upload program, and (\) is used as the file separator, the uploaded file storage directory specified on the debugging machine is: define('ROOT',dirname( __FILE__)."\upload")
, debugging locally is normal, but when uploading to the Linux server, errors will occur. This problem lies in the file separator. On Windows, \ is customarily used as the file separator, but on Linux, only "/" is recognized.
So we need to introduce the following PHP built-in variableDIRECTORY_SEPARATOR
.
For more related php knowledge, please visit php tutorial!
The above is the detailed content of Summary of PHP directory operations. For more information, please follow other related articles on the PHP Chinese website!