摘要:
遍历目录,输出所有文件文件夹(递归)123456789101112131415161718192021222324252627282930313233343536373839404...
遍历目录,输出所有文件文件夹(递归)
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 | <?php header("content-type:text/html;charset=utf-8"); include_once('./getfilesize.php'); date_default_timezone_set('PRC'); function dirtab ($dir) { if (!file_exists($dir)) { echo "目录不存在"; return false; } if (!is_dir($dir)) { echo "不是目录"; return false; } $res = opendir($dir); static $str ='';//如果不使用static,那么只输出一层,下一层的目录内容不输出 $num =0; while (false !== ($filename = readdir($res))) { $num++; $file_path = rtrim($dir,'/').'/'.$filename; if($filename == '.' || $filename == '..'){ continue; } if(is_dir($file_path)){ dirtab($file_path); } $color = $num % 2 == 0 ? '#abcdef': '#fff'; $str .= "<tr bgcolor=".$color.">"; $str .= "<td>".$filename."</td>"; $str .= "<td>".getfilesize(filesize($file_path))."</td>"; $str .= "<td>".(filetype($file_path) == 'dir' ? '目录':'文件') ."</td>"; $str .= "<td>".(date('Y-m-d H:i:s' , filectime($file_path))) ."</td>"; $str .= "<td>".(date('Y-m-d H:i:s' , filemtime($file_path))) ."</td>"; $str .= "<td>".(is_readable($file_path) == 1 ?'YES' : 'NO') ."</td>"; $str .= "</tr>"; } closedir($res); return $str; } echo "<table border='1' align='center' cellspacing='0'>"; echo "<tr>"; echo "<th>文件名</th>"; echo "<th>文件大小</th>"; echo "<th>类型</th>"; echo "<th>创建时间</th>"; echo "<th>修改时间</th>"; echo "<th>是否可读</th>"; echo "</tr>"; echo dirtab ('../1127'); echo "</table>"; |
遍历目录,输出所有文件(递归)
1 2 3 4 5 6 7 8 9 10 11 12 | //加一个输出判断is_file() if(is_file($file_path)){ $color = $num % 2 == 0 ? '#abcdef': '#fff'; $str .= "<tr bgcolor=".$color.">"; $str .= "<td>".$filename."</td>"; $str .= "<td>".getfilesize(filesize($file_path))."</td>"; $str .= "<td>".(filetype($file_path) == 'dir' ? '目录':'文件') ."</td>"; $str .= "<td>".(date('Y-m-d H:i:s' , filectime($file_path))) ."</td>"; $str .= "<td>".(date('Y-m-d H:i:s' , filemtime($file_path))) ."</td>"; $str .= "<td>".(is_readable($file_path) == 1 ?'YES' : 'NO') ."</td>"; $str .= "</tr>"; } |