Home >Backend Development >PHP Tutorial >Directory traversal to find implementation code in php_PHP tutorial

Directory traversal to find implementation code in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:59:361097browse

This article uses two important directory operation functions in php to realize the implementation method of traversing the directories and files in the directories. We have introduced the functions in detail. Let’s look at the examples below

glob() function returns the file name or directory matching the specified pattern.
The code is as follows
 代码如下 复制代码

function listFiles($path){
$result = array();
foreach(glob($path.''."*") as $item){
$result[strtolower($item)] = $item;
if(is_dir($item)){
$result += listFiles($item);
}
}
return $result;
}
$path = 'E:webdianle';
foreach(listFiles($path) as $item){
echo $item.'
';
}


function listFiles($path){
$result = array();
foreach( scandir($path) as $item ){
if($item != '.' && $item != '..' ){
$item = $path.''.$item;
$result[strtolower($item)] = $item;
if(is_dir($item)){
$result += listFiles($item);
}
}
}
return $result;
}
$path = 'E:webdianle';
foreach(listFiles($path) as $item){
echo $item.'
';
}
?>

Copy code

function listFiles($path){
$result = array();
foreach(glob($path.''."*") as $item){
$result[strtolower($item)] = $item;
if(is_dir($item)){
$result += listFiles($item);
}
}
return $result;
}
$path = 'E:webdianle';
foreach(listFiles($path) as $item){
echo $item.'
';
}


function listFiles($path){
$result = array();
foreach( scandir($path) as $item ){
if($item != '.' && $item != '..' ){
$item = $path.''.$item;
$result[strtolower($item)] = $item;
if(is_dir($item)){
$result += listFiles($item);
}
}
}
return $result;
}
$path = 'E:webdianle';
foreach(listFiles($path) as $item){
echo $item.'
';
}
?>

scandir(directory,sort,context)

A function used

参数 描述
directory 必需。规定要扫描的目录。
sort 可选。规定排列顺序。默认是 0 (升序)。如果是 1,则为降序。
context 可选。规定目录句柄的环境。context 是可修改目录流的行为的一套选项
The scandir() function returns an array containing the files and directories in the specified path.

If successful, return an array, if failed, return false. If

directory

is not a directory, returns Boolean false and generates an E_WARNING level error.

Grammar

glob(pattern,flags)

参数 描述
file 必需。规定检索模式。
size

可选。规定特殊的设定。

  • GLOB_MARK - 在每个返回的项目中加一个斜线
  • GLOB_NOSORT - 按照文件在目录中出现的原始顺序返回(不排序)
  • GLOB_NOCHECK - 如果没有文件匹配则返回用于搜索的模式
  • GLOB_NOESCAPE - 反斜线不转义元字符
  • GLOB_BRACE - 扩充 {a,b,c} 来匹配 'a','b' 或 'c'
  • GLOB_ONLYDIR - 仅返回与模式匹配的目录项
  • GLOB_ERR - 停止并读取错误信息(比如说不可读的目录),默认的情况下忽略所有错误

注释:GLOB_ERR 是 PHP 5.1 添加的

Now look at another function
This function returns an array containing matching files/directories. Returns false if an error occurs.

Grammar
http://www.bkjia.com/PHPjc/631303.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631303.htmlTechArticleThis article uses two important directory operation functions in php to traverse the directories and directories under the directory We have introduced the implementation method and function of the file below in detail. Let’s look at the actual implementation...
Parameters Description
file Required. Specifies the search mode.
size Optional. Specifies special settings.
  • GLOB_MARK - Add a slash to each returned item
  • GLOB_NOSORT - Return files in their original order of appearance in the directory (not sorted)
  • GLOB_NOCHECK - Returns the pattern used to search if no files match
  • GLOB_NOESCAPE - backslash does not escape metacharacters
  • GLOB_BRACE - expands {a,b,c} to match 'a', 'b' or 'c'
  • GLOB_ONLYDIR - Return only directory entries matching the pattern
  • GLOB_ERR - Stop and read error messages (such as unreadable directories), ignore all errors by default
Note: GLOB_ERR was added in PHP 5.1
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