Hongmu Notes
Home Language Notes PHP: Delete files with a specified file extension from a given directory.
Language Notes PHP

PHP: Delete files with a specified file extension from a given directory.

PHP: Delete files with a specified file extension from a given directory.

You can use the `glob` function in PHP to match file names within a specified directory, and then use the `unlink` function to delete those files.

The specific operating steps are as follows:

  1. Use the `glob` function to match file names within a specified directory and store the results in an array; the code is as follows:
    $files = glob('/path/to/dir/*.txt');

    Here, `/path/to/dir` refers to the directory to be operated on, and `*.txt` refers to the file name pattern to match – in this case, all files with the `.txt` extension.

  2. Iterate over the file array and call the `unlink` function on each file to delete it; the code is as follows:

    foreach ($files as $file) {
        unlink($file);
    }

    The complete code is as follows:

    $files = glob('/path/to/dir/*.txt');
    foreach ($files as $file) {
        unlink($file);
    }

The above code will delete all files with the.txt extension in the specified directory.

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

PHP ob function record

PHP ob function record Language Notes PHP

Usage of the following three functions ob_get_contents(); ob_end_clean(); ob_start(); You can use these functions to buffer local files and execute local script code. Use ob_start() to save the output code into the buffer, and the page will not be displayed; then use ob_get_contents to get the data in the buffer. o…
👁 141
PHP preg

PHP preg Language Notes PHP

The preg_match_all function is used to perform a global regular expression match. preg_match_all() Syntax int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG…
👁 169
Detailed explanation of PHP ternary operator and if

Detailed explanation of PHP ternary operator and if Language Notes PHP

Ternary operator condition ? Result 1 : Result 2 Explanation: The position in front of the question mark is the condition for judgment. If the condition is met, the result is 1, and if it is not met, the result is 2. This article compares and explains the ternary operator and if...else... in detail. I hope it will be helpful to everyone. Today when I was revising my paper online, I encountered a statement that I couldn’t understand: $if_summary = $row['IF_SUMMARY']=…
👁 189
PHP cast type

PHP cast type Language Notes PHP PHP collection PHP and mysql

Get the data type 1. If you want to check the value and type of an expression, use var_dump(). 2. If you just want to get an easy-to-read type expression for debugging, use gettype(). 3. To check a certain type, do not use gettype(), but use the is_type() function. Converting Strings to Numbers When a string is evaluated as a number, the result is determined according to the following rules...
👁 232

Recommended reading

(Adaptive Mobile Version) Electrical Sensor PBootCMS Website Template – 1085

(Adaptive Mobile Version) Electrical Sensor PBootCMS Website Template – 1085 Practical Collection pbootcms Template

An electrical sensor PbootCMS website template supporting both PC and WAP devices. Featuring a modern, precision-oriented design ideal for showcasing electrical sensors, automation products, and technical solutions. It enables industrial automation enterprises to present their products online and attract manufacturing and engineering clients. Template Overview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn | Related Articles...
👁 47
(Adaptive Mobile Version) Environmental Protection Equipment Website Template – Responsive Water Treatment Equipment Website Source Code Download: 0366

(Adaptive Mobile Version) Environmental Protection Equipment Website Template – Responsive Water Treatment Equipment Website Source Code Download: 0366 Practical Collection pbootcms Template

An adaptive mobile-friendly website template designed for environmental protection and water treatment equipment. Featuring a fresh, tech-driven design style, this template is ideal for showcasing water treatment systems, environmental solutions, and project cases. It enables environmental protection enterprises to demonstrate their technical expertise online and attract potential customers. Template Overview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn | Related Articles: PbootCM...
👁 46
Firefighting and Security Equipment Website Template 0655

Firefighting and Security Equipment Website Template 0655 Practical Collection Yiyou template

This EyouCMS template is ideal for the fire safety, firefighting, and security products industry. Its eye-catching and professional design effectively showcases fire safety products, security equipment, security solutions, and corporate strength. It enables security and fire safety companies to showcase their products online, attracting both corporate and government procurement opportunities. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS...
👁 33
(Adaptive mobile version) Simple, versatile corporate website template – Download PBootCMS corporate website source code (ID: 1086)

(Adaptive mobile version) Simple, versatile corporate website template – Download PBootCMS corporate website source code (ID: 1086) Practical Collection pbootcms Template

A simple, versatile enterprise PbootCMS website template compatible with both PC and WAP devices. Featuring a minimalist, modern design, it is ideal for small and medium-sized businesses to quickly create professional brand websites. It serves as an ideal foundational solution for cost-effective, high-efficiency website development. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn | Related Articles: PbootCMS...
👁 36
There are too many articles in Empire CMS and the backend is stuck like a dog. How to solve it?

There are too many articles in Empire CMS and the backend is stuck like a dog. How to solve it? Program Notes Empire cms

Recently, the Empire CMS database contained millions of articles; when I tried to load a page via the backend, I noticed that the page was extremely laggy – almost every page experienced similar performance issues. So, I began investigating the problem. The root cause turned out to be a specific PHP file used for refreshing pages in Empire CMS; as the number of articles grew, loading this PHP file for page refreshes became significantly slower. The solution was to locate that specific page-refreshing file and optimize it. However, after resolving the issue, I had somehow forgotten to record the filename – now I need to find it again...
👁 382