PHP listing the code for all files in a directory _php tutorial

Source: Internet
Author: User
Copy CodeThe code is as follows:
function Dir_path ($path) {
$path = str_replace (' \ \ ', '/', $path);
if (substr ($path,-1)! = '/') $path = $path. '/';
return $path;
}
/**
* List all the files in the directory
*
* @param str $path Directory
* @param str $exts suffix
* @param array $list path
* @return Array return path
*/
function Dir_list ($path, $exts = ", $list = Array ()) {
$path = Dir_path ($path);
$files = Glob ($path. '*');
foreach ($files as $v) {
if (! $exts | | preg_match ("/\. ( $EXTS)/I ", $v)) {
$list [] = $v;
if (Is_dir ($v)) {
$list = Dir_list ($v, $exts, $list);
}
}
}
return $list;
}
?>

How to use:
Copy CodeThe code is as follows:
$r = dir_list (' dir ');
printf ("

The output data is:

%s
\ n ", Var_export ($r, true));
?>


PHP Function-used to list all files under directory 2

A function written in PHP to list all the files in the specified directory.
The function is followed by a sample code that is used.
Note: If the page is Utf-8, in the Windows Chinese version of the system, read the Chinese file name will appear garbled.
Copy CodeThe code is as follows:
/* Function listdirtree ($dirName = null)
* * Feature lists all files and subdirectories under the directory
* * Parameter $dirName directory name
* * return directory structure array false for failure
*/
function Listdirtree ($dirName = null)
{
if (empty ($dirName))
Exit ("Ibfilesystem:directory is empty.");
if (Is_dir ($dirName))
{
if ($dh = Opendir ($dirName))
{
$tree = Array ();
while (($file = Readdir ($DH))!== false)
{
if ($file! = "." && $file! = "...")
{
$filePath = $dirName. "/" . $file;
if (Is_dir ($filePath))//for directory, recursive
{
$tree [$file] = Listdirtree ($filePath);
}
else//is a file that is added to the current array
{
$tree [] = $file;
}
}
}
Closedir ($DH);
}
Else
{
Exit ("Ibfilesystem:can not open Directory $dirName.");
}
Returns the current $tree
return $tree;
}
Else
{
Exit ("Ibfilesystem: $dirName is not a directory.");
}
}
$files = Listdirtree (".");
Print_r ($files);
$size = count (files);
The following code creates a list of files in this directory (with a link address)
Echo '
      ';
      for ($i =0; $files [$i]! = NULL; $i + +) {
      Echo '
    1. '. $files [$i]. '
    2. ';
      }
      Echo '
';
?>

http://www.bkjia.com/PHPjc/326054.html www.bkjia.com true http://www.bkjia.com/PHPjc/326054.html techarticle Copy the code as follows: Php function Dir_path ($path) {$path = str_replace (' \ \ ', '/', $path), if (substr ($path,-1)! = '/') $path = $path. '/'; return $path; }/** * List directory ...

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