Home > Article > Backend Development > visitseoul php visitFile traverses the specified folder function
Note: visitFile() has a few modifications
Copy the code The code is as follows:
// View the files in the specified folder
$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); // Replace multiple backslashes
$fileList[] = is_dir($pathSub) ? $pathSub."/" : $pathSub;
if(is_dir($pathSub )){ visitFile($pathSub); }
}
$fdir->close();
return $fileList;
}
?>