php visitFile()遍曆指定檔案夾函數
來源:互聯網
上載者:User
注:visitFile()有少量修改
複製代碼 代碼如下:<?
// 查看指定檔案夾的檔案
$fileList = array();
function visitFile($path)
{
global $fileList;
$path = str_replace("\\", "/", $path);
$fdir = dir($path);
while (($file = $fdir->read()) !== false)
{
if($file == '.' || $file == '..'){ continue; }
$pathSub = preg_replace("*/{2,}*", "/", $path."/".$file); // 替換多個反斜線
$fileList[] = is_dir($pathSub) ? $pathSub."/" : $pathSub;
if(is_dir($pathSub)){ visitFile($pathSub); }
}
$fdir->close();
return $fileList;
}
?>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<form method="get">
<?
$path = str_replace("\\", "/", $path);
$path = preg_replace("*/{2,}*", "/", $path);
?>
路徑:<input type="text" name="path" id="path" value="<?=$path;?>"/><br>
<li>磁碟根目錄 /</li>
<li>網路本地 ./phpMyAdmin</li>
<li>本地磁碟 file://C: 或者 C:</li>
<br>
<input name="action" type="submit" id="action" value="view" />
<input name="action" type="submit" id="action" value="delete" onclick="if(!confirm('是否刪除 '+path.value+' 的所有子檔案夾和子檔案?')) return false;" />
</form>
<?
if(!empty($path)){
$path = preg_replace("*/{2,}*", "/", $path);
$files = visitFile($path);
switch(strtolower($_GET["action"]))
{
case "view":
foreach($files as $key => $value)
{
printf("No.%4d·%s<br>\r\n", $key+1, $value);
}
break;
case "delete":
$faileFiles = array();
foreach(array_reverse($files) as $value)
{
if(!unlink($value))
{
array_push($faileFiles, $value);
}
}
if(!unlink($path)) { array_push($faileFiles, $path); }
if(count($faileFiles) > 0)
{
printf("<br><br>刪除失敗檔案(%d):<p>\r\n", count($faileFiles));
foreach( $faileFiles as $key => $value)
{
printf("No.%4d·%s<br>\r\n", $key+1, $value);
}
}
break;
}
}
?>