PHP implementation code to obtain all directories and files contained in a directory
Release: 2016-07-25 08:57:48
Original
917 people have browsed it
-
- /**
- * Get all directories and files contained in the input directory
- * Return as an associative array
- * edit: bbs.it-home.org
- */
- function deepScanDir($dir)
- {
- $fileArr = array();
- $dirArr = array();
- $dir = rtrim($dir, '//');
- if(is_dir($dir)){
- $dirHandle = opendir($dir);
- while(false !== ($fileName = readdir($dirHandle))){
- $subFile = $dir . DIRECTORY_SEPARATOR . $fileName;
- if(is_file($subFile)){
- $fileArr[] = $subFile;
- } elseif (is_dir($subFile) && str_replace('.', '', $fileName)!=''){
- $dirArr[] = $subFile;
- $arr = deepScanDir($subFile);
- $dirArr = array_merge($dirArr, $arr['dir']);
- $fileArr = array_merge($fileArr, $arr['file']);
- }
- }
- closedir($dirHandle);
- }
- return array('dir'=>$dirArr, 'file'=>$fileArr);
- }
- //示例
- $dir = '/var/htdocs/w4/article';
- $arr = deepScanDir($dir);
- print_r($arr);
-
- /**
- * Get all files contained in the input directory
- * Return as an array
- * author: flynetcn
- */
- function get_dir_files($dir)
- {
- if (is_file($dir)) {
- return array($dir);
- }
- $files = array();
- if (is_dir($dir) && ($dir_p = opendir($dir))) {
- $ds = DIRECTORY_SEPARATOR;
- while (($filename = readdir($dir_p)) !== false) {
- if ($filename=='.' || $filename=='..') { continue; }
- $filetype = filetype($dir.$ds.$filename);
- if ($filetype == 'dir') {
- $files = array_merge($files, get_dir_files($dir.$ds.$filename));
- } elseif ($filetype == 'file') {
- $files[] = $dir.$ds.$filename;
- }
- }
- closedir($dir_p);
- }
- return $files;
- }
复制代码
|
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31