Home > Backend Development > PHP Tutorial > php opendir() function finds all files in a directory

php opendir() function finds all files in a directory

怪我咯
Release: 2023-03-13 12:38:01
Original
1578 people have browsed it

opendir() 函数打开目录句柄。readdir() 函数返回目录中下一个文件的文件名。closedir() 函数关闭目录句柄。这篇文章主要介绍了php opendir()列出目录下所有文件的实例代码的相关资料,需要的朋友可以参考下

实例一:

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

<?php

   $dr = @opendir(&#39;/tmp/&#39;);
   if(!$dr) {
     echo "Error opening the /tmp/ directory!<BR>";
     exit;
   }

   while(($files[] = readdir($dr)) !== false);

   print_r($files);
?>
Copy after login

实例二:

列出目录下所有文件

<?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>
Copy after login

The above is the detailed content of php opendir() function finds all files in a directory. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template