Home > Article > Backend Development > How to get the first-level directory of a file in php (pure code)
The content of this article is about how to obtain the first-level directory of files (pure code) in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
php obtains the first-level example code of the file directory (solve the problem of Chinese garbled characters);
// 获取目录一层的方法 /* * @param readfileone //读取一层文件夹内容 * @param dir // 查找的路径 * @param * @param */ function readfileone($dirName){ $array=array(); $dir='./Public/Uploads/'.$dirName; if(is_dir(iconv('utf-8','gb2312',$dir))==true){ if($handle=opendir(iconv('utf-8','gb2312',$dir))){ while(false!==($files=readdir($handle))){ $files=iconv('gb2312','utf-8',$files); if($files!="."&&$files!=".."){ $files=iconv('utf-8','utf-8',$files); $dir=iconv('utf-8','utf-8',$dir); if(is_dir($dir."/".$files)) { $new_dir=$dir."/".$files; readfileone($new_dir); } else { $str=$dir."/".$files; $str=iconv('utf-8','utf-8',$str); // session('success.file',$str); array_push($array,$str); } } } } closeDir($handle); }else{ // echo '没有找到目录'; array_push($array,'没有找到目录'); array_push($array,'fail'); } return $array;//返回二维数组 }
Recommended related articles:
How to create php File Directory? (Pure code)
Code example of how PHP deletes a directory and all files in the directory
How PHP decompresses the compressed package file to a specified location Directory? (Pure code)
The above is the detailed content of How to get the first-level directory of a file in php (pure code). For more information, please follow other related articles on the PHP Chinese website!