php读取当前网站目录下的文件列表
IIS会限制用户直接访问网站的目录列表, 然而可以通过给当前目录下放一个index.php,然后编写代码, 实现访问.
这样就能直接拉取文件目录了, 而且可以配置不显示哪些文件.
这对于提供资料获取页面,有时候很有帮助.
index.php
将这个代码保存到你想展示的目录下即可. 记得更改名称为index供IIS自动运行.
可以根据需要修改.
<?php
$num=0; //用来记录目录下的文件个数
$dirname='./'; //要遍历的目录名字
$dir_handle=opendir($dirname);
echo '<table border="1" align="center" width="960px" cellspacing="0" cellpadding="0">';
echo '<caption><h2>目录'.$dirname.'的内容</h2></caption>';
echo '<tr align="left" bgcolor="#cccccc">';
echo '<th>序号</th><th>名称</th><th>大小</th><th>类型</th><th>修改时间</th></tr>';
while($file=readdir($dir_handle))
{
if($file!="."&&$file!="..")
{
$dirFile=$dirname."/".$file;
if($num++%2==0) //隔行换色
$bgcolor="#ffffff";
else
$bgcolor="#dddddd";
echo '<tr bgcolor='.$bgcolor.'>';
echo '<td>'.$num.'</td>';//序号
echo '<td><a href="'.$file.'" target="_blank">'.$file.'</a></td>'; //名称 链接
echo '<td>'.filesize($dirFile).' B</td>';//大小
echo '<td>'.filetype($dirFile).'</td>';//类型
echo '<td>'.date("Y/n/t",filemtime($dirFile)).'</td>';//修改时间
echo '</tr>';
}
}
echo '</table>';
closedir($dir_handle);
echo '在<b>'.$dirname.'</b>目录下的子目录和文件共有<b>'.$num.'</b>个';
?>
