php opendir()函数用于打开目录,通常与readdir()和closedir()函数一起用来读取目录下所有文件(即遍历目录),本文章向大家介绍php使用opendir()函数列出目录下所有文件的实例。
实例一:
使用opendir()列出目录下所有文件
<?php
$dr = @opendir('/tmp/');
if(!$dr) {
echo "Error opening the /tmp/ directory!<BR>";
exit;
}
while(($files[] = readdir($dr)) !== false);
print_r($files);
?>实例二:
列出目录下所有文件
立即学习“PHP免费学习笔记(深入)”;
<?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 opendir()列出目录下所有文件的实例代码相关文章请关注PHP中文网!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号