Home >Backend Development >PHP Tutorial >An example of how PHP uses one line of code to delete all files in a directory
This article mainly introduces you to relevant information about how PHP uses one line of code to delete all files in a directory. The article first gives a brief introduction to the glob function, and then introduces the deletion method in great detail through sample code. , friends in need can refer to it, let’s take a look below.
Preface
I think many people will write a few lines or even dozens of lines of code to list all files into an array for deletion, 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 ] )
glob() function follows libc glob() The rules used by the function find all file paths that match pattern, similar to the rules used by ordinary shells. No abbreviation expansion or parameter substitution is performed. Glob is powerful in using regular path matching.
flags Valid flags are:
GLOB_MARK - Add a slash
## to each returned itemExample 1
<?php print_r(glob("*.txt")); ?>The output is similar to:
Array ( [0] => target.txt [1] => source.txt [2] => test.txt [3] => test2.txt )
Example 2
<?php print_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 the directory All files
array_map('unlink', glob('*'));
Summary
You Articles that may be of interest:Explanation of how PHP obtains the first non-repeating character in a character stream
A brief discussion on PHP string reverse Questions often encountered in transfer interviews
Detailed explanation of the type declaration of functions in various versions of PHP
The above is the detailed content of An example of how PHP uses one line of code to delete all files in a directory. For more information, please follow other related articles on the PHP Chinese website!