PHP traversal file implementation code _php tips

Source: Internet
Author: User
Tags glob
Copy Code code as follows:

function Files ($path)
{
foreach (Scandir ($path) as $line)
{
if ($line = = '. ' | | $line = = '.. ') Continue
if (Is_dir ($path. ') /'. $line)) Files ($path. ' /'. $line);
else echo ' <li> '. $path. ' /'. $line. ' </li> ';
}
}

php traverse files and folders
Join a given folder C:\\windows\\apppatch
1. First get all the things under this folder, which is file, folder, put an array inside
$FILEARR = Array (
' Files ' => array (),//file put an array
' Dirs ' => Array (),//folder put an array
)
2. If there are subfolders, traverse subfolders, get folders and files, also put in that array, so loop, one does not leak
Copy Code code as follows:

<?php
$dir = ' f:\\game ';
function Read_dir_all ($dir) {
$ret = Array (' dirs ' =>array (), ' Files ' =>array ());
if ($handle = Opendir ($dir)) {
while (false!== ($file = Readdir ($handle))) {
if ($file!= '. ' && $file!== ' ... ') {
$cur _path = $dir. Directory_separator. $file;
if (Is_dir ($cur _path)) {
$ret [' dirs '] [$cur _path] = Read_dir_all ($cur _path);
} else {
$ret [' files '] = $cur _path;
}
}
}
Closedir ($handle);
}
return $ret;
}
$p = Read_dir_all ($dir);
Echo ' <pre> ';
Var_dump ($p);
Echo ' </pre> ';
?>

PHP traverses all directories and files in a folder
In the interview we often encounter this topic: PHP Traverse a folder of all the files and subfolders.
There are many ways to solve this problem. But the general idea is the same. Adopt recursion.
Copy Code code as follows:

$path = './filepath ';
function GetFiles ($path)
{
if (!is_dir ($path)) return;
$handle = Opendir ($path);
while (false!== ($file = Readdir ($handle)))
{
if ($file!= '. ' && $file!= ' ... ')
{
$path 2= $path. ' /'. $file;
if (Is_dir ($path 2))
{
Echo ';
Echo $file;
GetFiles ($path 2);
}else
{
Echo ';
Echo $file;
}
}
}
}
Print_r (GetFiles ($path));
Echo ' <HR> ';
function Getdir ($path)
{
if (!is_dir ($path)) return;
$handle = Dir ($path);
while ($file = $handle->read ())
{
if ($file!= '. ' && $file!= ' ... ')
{
$path 2 = $path. ' /'. $file;
if (Is_dir ($path 2))
{
echo $file. " \ t ";
Getdir ($path 2);
}else
{
echo $file. ' ';
}
}
}
}
Getdir ($path);
Echo ' <HR> ';
function Get_dir_scandir ($path) {
$tree = Array ();
foreach (Scandir ($path) as $single) {
if ($single!= '. ' && $single!= ' ... ')
{
$path 2 = $path. ' /'. $single;
if (Is_dir ($path 2))
{
echo $single. " \ r \ n ";
Get_dir_scandir ($path 2);
}else
{
echo $single. " \ r \ n ";
}
}
}
}
Get_dir_scandir ($path);
Echo '
<HR> ';
function Get_dir_glob () {
$tree = Array ();
foreach (Glob ('./curl/* ') as $single) {
echo $single. " \ r \ n ";
}
}
Get_dir_glob ();
Echo '
<HR> ';
function Myscandir ($path)
{
if (!is_dir ($path)) return;
foreach (Scandir ($path) as $file)
{
if ($file!= '. ' && $file!= ' ... ')
{
$path 2= $path. ' /'. $file;
if (Is_dir ($path 2))
{
Echo $file;
Myscandir ($path 2);
}else
{
echo $file. ' ';
}
}
}
}
Myscandir ($path);
Echo ' <HR> ';
function Myglob ($path)
{
$path _pattern = $path. ' /*';
foreach (Glob ($path _pattern) as $file)
{
if (Is_dir ($file))
{
Echo $file;
Myscandir ($file);
}else
{
echo $file. ' ';
}
}
}
Myglob ($path);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.