visitseoul php visitFile遍历指定文件夹函数
程序员文章站
2022-05-12 19:29:14
...
注:visitFile()有少量修改
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
\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("
删除失败文件(%d):
复制代码 代码如下:
// 查看指定文件夹的文件
$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;
}
?>
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
\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("
删除失败文件(%d):
\r\n", count($faileFiles));
foreach( $faileFiles as $key => $value)
{
printf("No.%4d·%s
\r\n", $key+1, $value);
}
}
break;
}
}
?>
以上就介绍了visitseoul php visitFile遍历指定文件夹函数,包括了visitseoul方面的内容,希望对PHP教程有兴趣的朋友有所帮助。