Example of how PHP uses one line of code to delete all files in a directory

韦小宝
Release: 2023-03-19 14:00:02
Original
1393 people have browsed it

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 very well through the PHP sample code. For details, friends who are interested in PHP can refer to it. Let’s take a look below.

Preface

Presumably many people will write a few lines or even dozens of lines of code to list all files into an arrayDelete, 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 ] )
Copy after login

glob() function searches for all file paths matching pattern according to the rules used by libc glob() function , similar to the rules used by general 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 item
  • #GLOB_NOSORT - Return the files in their original order of appearance in the directory (not sorted)

  • GLOB_NOCHECK - Return if no files match for

    search's pattern

  • GLOB_NOESCAPE - Backslash not escaped

    metacharacter

  • ##GLOB_BRACE - Extended {a, b,c} to match 'a', 'b' or 'c'
  • GLOB_ONLYDIR - Return only directory entries matching pattern
  • GLOB_ERR - Stop and read
  • error messages

    (e.g. unreadable directories), by default ignore all errors

Example 1##

<?php
print_r(glob("*.txt"));
?>
Copy after login
The output is similar to:

Array
(
[0] => target.txt
[1] => source.txt
[2] => test.txt
[3] => test2.txt
)
Copy after login


Example 2

<?php
print_r(glob("*.*"));
?>
Copy after login
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
)
Copy after login

Delete all files in the directory

array_map(&#39;unlink&#39;, glob(&#39;*&#39;));
Copy after login

The above is all the content of this article, I hope it will be helpful to everyone's learning ! !

Related recommendations:

php function array_pop() to delete the last element in the array


php delete string Trailing whitespace character function

Detailed explanation of how to delete a specified folder in PHP

The above is the detailed content of 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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template