PHP reads all files in a directory

Source: Internet
Author: User
/**
* Get all directories and files included in the input directory
* Returned as an associative array
* AUTHOR:FLYNETCN
*/
function Deepscandir ($dir)
{
$FILEARR = Array ();
$DIRARR = Array ();
$dir = RTrim ($dir, '//');
if (Is_dir ($dir)) {
$dirHandle = Opendir ($dir);
while (false!== ($fileName = Readdir ($dirHandle))) {
$subFile = $dir. Directory_separator. $fileName;
if (Is_file ($subFile)) {
$FILEARR [] = $subFile;
} elseif (Is_dir ($subFile) && str_replace ('. ', ', $fileName)! = ') {
$DIRARR [] = $subFile;
$arr = Deepscandir ($subFile);
$DIRARR = Array_merge ($DIRARR, $arr [' dir ']);
$FILEARR = Array_merge ($FILEARR, $arr [' file ']);
}
}
Closedir ($dirHandle);
}
Return Array (' dir ' = = $dirArr, ' file ' = $FILEARR);
}
Example
$dir = '/var/htdocs/w4/article ';
$arr = Deepscandir ($dir);
Print_r ($arr);
/**
* Get all the files contained in the input directory
* Returned as an array
* AUTHOR:FLYNETCN
*/
function Get_dir_files ($dir)
{
if (Is_file ($dir)) {
Return Array ($dir);
}
$files = Array ();
if (Is_dir ($dir) && ($dir _p = Opendir ($dir))) {
$ds = Directory_separator;
while ($filename = Readdir ($dir _p))!== false) {
if ($filename = = '. ' | | $filename = = ' ... ') {continue;}
$filetype = filetype ($dir. $ds. $filename);
if ($filetype = = ' dir ') {
$files = Array_merge ($files, Get_dir_files ($dir. $ds. $filename));
} elseif ($filetype = = ' file ') {
$files [] = $dir. $ds. $filename;
}
}
Closedir ($dir _p);
}
return $files;
}

The above describes the PHP read all the files under a directory, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.