Home  >  Article  >  Backend Development  >  php visitFile() traverses the specified folder function_PHP tutorial

php visitFile() traverses the specified folder function_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:34:00935browse

Note: visitFile() has a few modifications

Copy code The code is as follows:

// View the specification Files in the 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;
}
?>


$path = str_replace("\", "/" , $path);
$path = preg_replace("*/{2,}*", "/", $path);
?>
Path:

  • Disk root directory/

  • Network local./phpMyAdmin

  • Local disk file://C: or C:






  • 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
    rn", $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 ("

    Deletion failed file (%d):

    rn", count($faileFiles));
    foreach( $faileFiles as $key => $value)
    {
    printf("No.%4d·%s
    rn", $key+1, $value);
    }
    }
    break;
    }
    }
    ?>

    www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322470.htmlTechArticleNote: visitFile() has a few modifications. The copy code is as follows: ? // View the files in the specified folder $fileList = array(); function visitFile($path) { global $fileList; $path = str_replace(...
    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn