php visitFile()遍历指定文件夹函数
程序员文章站
2022-10-06 19:14:14
注:visitfile()有少量修改 复制代码 代码如下: // 查看指定文件夹的文件 $filelist = array(); function visitf...
注: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;
}
}
?>
复制代码 代码如下:
<?
// 查看指定文件夹的文件
$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;
}
}
?>