为了不让别人看到自己服务器目录下面有什么文件,同时又不能修改服务器配置的同时,给每个目录下生成一个默认的索引文件,就成为了一个最好的办法,下面的函数,就是在指定目录的所有子目录下(包含指定目录),生成一个index.html。
<?php
/**
* 为每个目录下拷贝一个index.html
*
* @param string $dir
* @param string $sourcefile 此路径必须为绝对路径
*/
function copy_index($dir=”, $sourcefile=”)
{
if (!is_dir($dir)) $dir = ‘.’; //如果指定目录出错默认为当前目录
$dir = str_replace(‘\\’, ‘/’, $dir); //转成兼容linux的路径
$dir = substr($dir, -1) == ‘/’ ? $dir : $dir.’/'; //整合成最后为/的路径
$files = scandir($dir); //读取当前目录下文件
foreach ($files as $file)
{
if (!is_dir($dir.$file)) continue;
if (substr($file, 0, 1) == ‘.’) continue; //隐藏目录跳过
copy_index($dir.$file, $sourcefile);
}
if (!is_file($dir.’index.html’)) copy($sourcefile, $dir.’index.html’); //拷贝文件
}
//函数调用
$sourcefile = str_replace(‘\\’, ‘/’ ,dirname(__FILE__)).’/index.html’; //此为与函数copy_index所在文件同级目录下的index.html
copy_index(‘./’, $sourcefile);
