您当前的位置:首页 > 网站建设 > php
| php | asp | css | H5 | javascript | Mysql | Dreamweaver | Delphi | 网站维护 | 帝国cms | React | 考试系统 | ajax | jQuery |

php 使用opendir()列出目录下所有文件的实例代码

51自学网 2019-09-09 10:24:42
opendir()   php

php opendir()函数用于打开目录,通常与readdir()和closedir()函数一起用来读取目录下所有文件(即遍历目录),本文章向大家介绍php使用opendir()函数列出目录下所有文件的实例。

实例一:

使用opendir()列出目录下所有文件

?
1
2
3
4
5
6
7
8
9
10
11
12
<?php
 
   $dr = @opendir('/tmp/');
   if(!$dr) {
     echo "Error opening the /tmp/ directory!<BR>";
     exit;
   }
 
   while(($files[] = readdir($dr)) !== false);
 
   print_r($files);
?>

实例二:

列出目录下所有文件

?
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
<?php 
 $dirname = "C:\\Apache\\bin";
 $dir = opendir( $dirname );
  
 while( false != ( $file = readdir( $dir ) ) )
 {
  if( ( $file != "." ) and ( $file != ".." ) )
  {
   $file_list .= "<li>$file</li>";
  }
 }
 closedir( $dir );
?>
 
<html>
 <head>
 <title>列出目录下所有文件</title>
 <head>
 <body>
 <p>Files in <?php echo( $dirname ); ?> </p>
 <ul>
  <?php echo( $file_list ); ?>
 </ul>
 </body>
</html>

下载地址:
php在提交页面使用下拉框
docx 正确Microsoft Office MIME类型
51自学网,即我要自学网,自学EXCEL、自学PS、自学CAD、自学C语言、自学css3实例,是一个通过网络自主学习工作技能的自学平台,网友喜欢的软件自学网站。
京ICP备13026421号-1