PHP uses a single line of code to delete all file method examples explained in the directory

Source: Internet
Author: User
Tags glob types of functions
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

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.