This article mainly introduces you about how to use a line of code to delete all the files in the directory, the article first a simple introduction to the Glob function, and then through the sample code to remove the method introduced very detailed, the need for friends can refer to the following to see it together.
Objective
Presumably a lot of people would write a few lines or even dozens of lines of code to make it list all the files into an array to delete, but the Glob function solves the problem in minutes! Let's take a look at the detailed introduction below.
Glob Syntax Description:
Array Glob (string $pattern [, int $flags = 0])
The Glob () function looks for all file paths that match the pattern in accordance with the rules used by the libc glob () function, similar to the rules used for general shells. No abbreviation extensions or parameter overrides are performed. Glob is powerful with regular matching paths.
Flags valid tokens are:
Glob_mark-Add a slash to each returned item
Glob_nosort-Returns (not sorted) according to the original order in which the files appear in the directory
Glob_nocheck-Returns the mode used for search if no file matches
Glob_noescape-Backslash does not escape meta-characters
Glob_brace-expand {a,b,c} to match ' A ', ' B ' or ' C '
Glob_onlydir-Returns only catalog entries that match the pattern
Glob_err-Stop and read error messages (such as unreadable directories), ignoring all errors by default
Example 1
<?phpprint_r (Glob ("*.txt"));? >
The output is similar to:
Array ([0] = target.txt[1] = source.txt[2] = test.txt[3] = test2.txt)
Example 2
<?phpprint_r (Glob ("* *"));? >
The output is similar to:
Array ([0] = contacts.csv[1] (= default.php[2] = target.txt[3] = source.txt[4] = tem1.tmp[5] = Test. HTM[6] = test.ini[7] [test.php[8] = test.txt[9] = test2.txt)
Delete all files under directory
Array_map (' unlink ', glob (' * '));
Summarize
Articles you may be interested in:
PHP gets the first non-repeating character in a character stream by explaining
On the problems frequently encountered in the interview of PHP string reversal
A detailed description of the types of functions in PHP versions