PHP读取文件目录, 并显示需要的目录

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>目录</title><style>
 6 a {
 7     text-decoration: none;
 8 }
 9     </style>
10 </head>
11 <body>
12     <?php 
13 
14         function get_file_type($file) {
15             $type = explode('.', $file);
16             $count = count($type)-1;
17             return $type[$count];
18         }
19 
20         function show_ol($arr)
21         {
22             $str = '<ol>';
23             for ($i=0; $i < count($arr) ; $i++) { 
24                 $str .= '<li><a target="_blank" href="'.$arr[$i].'">'. $arr[$i] . '</a></li>';
25             }
26             $str .= '</ol>';
27             return $str;
28         }
29 
30         $all_dir = dirname(__FILE__);
31         $files = scandir($all_dir);
32         $fies_array = array('php', 'htm');
33 
34         $f_array = array();
35         foreach ($files as $key => $file) {
36             if( !($file == '.' || $file == '..' || is_dir($file) || $file == '.DS_Store') ) {
37                 if(  !in_array(get_file_type($file), $fies_array)  ) {
38                     array_push($f_array, $file);     
39                 }
40             }
41         }
42         $head = '<h1>我的文件目录</h1>';
43         $str = show_ol($f_array);
44         echo $head . $str;
45      ?>
46 </body>
47 </html>

结果:

原文地址:https://www.cnblogs.com/fangdada/p/5287916.html