Directory Traversal in PHP tutorial

Source: Internet
Author: User
Tags filter foreach array glob php tutorial resource

Introduction to HP Learning Tutorial article: Directory Traversal in PHP in the process of writing a PHP application, the traversal of the specified directory and the filtering of the files are essential, and PHP itself provides a powerful way to parse and read the directory. These methods are firmly grasped by every phper

Directory Traversal in PHP tutorial

In the process of writing a PHP application, the traversal of the specified directory and the filtering of the files are essential, and PHP itself provides a powerful way to parse and read the directory. It is necessary for every phper to have a firm grasp of these methods. This article will summarize and sum up several important methods, the author's experience is still shallow, if there are errors and omissions hope netizens to correct, with a view to common progress.


function return type description
Opendir resource handle to get resource handle for specified directory
Is_dir Boolean to determine if it is a directory
Is_readable boolean given directory is readable
The Readdir string reads the file name of a file through the resource handle, and the pointer moves back if it does not exist or has returned false at the last word
Closedir void closes the resource handle and frees the resource
Scandir Array returns an array of all files and directories under the specified directory



Traverse a file in a specified folder



This code completes the basic traversal of files and directories in the specified directory, where the Readdir function reads the file in the given directory resource handle $handle and moves the pointer back, False if the last file has been reached. Another "." Represents this level of directory "..." Representing the upper directory will also be listed, and in most cases we need to filter them out.



Traverses a specified type of file under a specified folder

In most cases, our traversal of the directory is often limited by certain conditions, such as simply traversing the image file or all the XML files in the specified directory. To meet the above requirements, we rewrite the code and encapsulate the code in a function. The function can filter out "." and ".." And the directory, and the root parameter returns the file for the specified extension.





The above function to obtain a file of the specified type in the specified directory. Call this function if we want to get all of the JPEG and PNG files under Mypics:

$temp = Getfilefrom ("./mypics", Array (' JPEG ', ' png '));

Recursive traversal of a directory tree
I think the most useful recursive function in PHP is a recursive traversal of the directory, and the following function can get the complete structure of the specified directory. At the same time using Scandir instead of Opendir to simplify the code.





Magical Glob.
Glob is a very powerful function after php4.3.0, but the official manual does not provide a detailed description. The function is defined as follows:

Array Glob (string $pattern [, int $flags = 0])

The glob has two parameters $pattern with an optional $flag. $pattern literal is a template rather than simply a specified directory of strings, $flag are some constant parameters that are used to change the behavior of Glob.

Learn his usage by practice. Our code1 is used to traverse a directory of all the files in the code, if written in Glob can do so.






We can see the difference, first glob to be much simpler, followed by Glob's first argument, "d:/wamp/*" tells Glob the file to look for is all the files in the "D:/wamp" directory, so $pattern is more like the operating system's wildcard character. And the returned content has filtered out '. ' and '. ', if we want to find all the BMP file as long as the parameters written as "D:/wamp/*.bmp" on it, this is much simpler. Take a common example, for example, our site has a folder called wallpaper, each of the following directory is wallpaper classification, and each category contains two catalogs, origin (normal size), and thumb (thumbnail), We need a piece of code to read all the thumbnails of all categories, and if it's not a piece of code that's going to be a hassle with glob, with Glob, the code will look like this:


foreach (Glob ("wallpaper/*/thumb/*") as $fileName) {
Ehco $fileName;
}


Glob the meaning of the second parameter:

Glob_mark adds a backslash for each directory
Glob_nosort results are not sorted
Glob_nocheck If there are no matching results, the template itself is returned
Glob_nospace backslashes does not quote metacharacters
Glob_brace {a,b,c} decomposed into a,b,c and matched separately (important)
Glob_onlydir only returns a table of contents
Glob_err Stop on Read errors (like unreadable directories) by default errors are.



If we're looking for a picture file, we can write that.

foreach (Glob ("D:/wamp/{*.jpeg,*.jpg,*.gif}", Glob_brace) as $filename) {
echo $filename;
}

The following code is a rewrite of the Getfilefrom () function, the code is much simpler, and the difficulty becomes the parsing of the $exts parameter.



The above is a simple summary of the directory traversal, is a personal learning notes, if also can help everyone, I am very happy.



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.