Hongmu Notes
Home Language Notes PHP: Delete all files in a specified directory.
Language Notes PHP

PHP: Delete all files in a specified directory.

PHP: Delete all files in a specified directory.

PHP can be used.glob()The function searches for all files in the current directory and then uses a loop to check, one by one, whether each file name should be deleted.unlink()The function removes unnecessary files.

Here is sample code:

<?php
$files = glob("*"); // 查找当前目录下的所有文件
foreach($files as $file) {
    if(is_file($file) && $file != "1.php" && $file != "2.php") {
        unlink($file); // 删除不需要的文件
    }
}
?>

This code is used first.glob("*")The function retrieves all files in the current directory and then uses a loop to check, one by one, whether each file name should be deleted.is_file()This function determines whether the current item is a file.$file != "1.php" && $file != "2.php"Used to determine whether the current file should be retained. If the current file does not need to be retained, then use it.unlink()The function deletes this file. Note:unlink()The function will delete files directly; therefore, use it with caution.

There are other directories in the current directory.

If there are additional directories within the current directory, recursively process these subdirectories to ensure that all unnecessary files are deleted.

Here is sample code:

 隐藏内容:登录后可查看
<?php
function deleteFiles($dir) {
    $files = glob("$dir/*"); // 查找当前目录下的所有文件和目录
    foreach($files as $file) {
        if(is_file($file) && $file != "$dir/1.php" && $file != "$dir/2.php") {
            unlink($file); // 删除不需要的文件
        } elseif(is_dir($file)) {
            deleteFiles($file); // 递归处理子目录
        }
    }
}

deleteFiles("."); // 删除当前目录下除了1.php和2.php之外的所有文件
?>

This code defines a variable nameddeleteFiles()A function that accepts a single parameter.$dirIndicates the directory where the file should be deleted. The function is used first.glob("$dir/*")The function retrieves all files and directories in the current directory, then uses a loop to check, one by one, whether each file name should be deleted. If the current item is a file and is not one that should be retained, then...unlink()The function removes this file. If the current item is a directory, it will be processed recursively.deleteFiles()The function processes this directory.

Finally, use.deleteFiles(".")The function call removes all files and subdirectories in the current directory, except for 1.php and 2.php.

微信赞赏

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) Responsive Website Template for Academic Papers, Journals, and Books – 1011

(Adaptive Mobile Version) Responsive Website Template for Academic Papers, Journals, and Books – 1011 Practical Collection pbootcms Template

A responsive PbootCMS website template designed for academic journals and book publications, compatible with both PC and WAP devices. Its professional, academic design makes it ideal for journals and academic publishing platforms to showcase their content and services. This template helps academic publishing institutions build an authoritative online presence and attract both authors and readers. Template Demo | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.c...
👁 40
(PC+WAP) Computer Operating System Software Download Website – pbootCMS Template; Windows System Software Download Website Source Code – 0075

(PC+WAP) Computer Operating System Software Download Website – pbootCMS Template; Windows System Software Download Website Source Code – 0075 Practical Collection pbootcms Template

A PbootCMS template designed specifically for websites focused on computer operating systems and software downloads, supporting both PC and WAP access. Its clean and efficient design makes it ideal for showcasing Windows systems, software applications, and technical tutorials. The template includes built-in download management and article/information modules, making it an ideal choice for creating software resource portals or technical blogs. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: a...
👁 36
Detailed step-by-step instructions for upgrading from EmpireCMS 7.2 to EmpireCMS 7.5!

Detailed step-by-step instructions for upgrading from EmpireCMS 7.2 to EmpireCMS 7.5! Program Notes Empire cms

Note: This upgrade procedure does not affect the data or templates from the original Version 7.2. During the upgrade process, I encountered SQL-related errors and issues where the second upgrade failed multiple times when using the official upgrade tool; therefore, I modified the official upgrade tool – specifically, by disabling the SQL error handling and removing the function responsible for the second upgrade – which allows the upgrade to proceed smoothly! Navigation: I. Pre-upgrade Backup Procedure; II. Upgrade Steps; III. Member Interface Upgrade Instructions...
👁 260
European Classical Furniture Website Template 0817

European Classical Furniture Website Template 0817 Practical Collection Yiyou template

An EyouCMS website template designed specifically for the European classical furniture and home furnishings industry. Its luxurious, classical design style is ideal for showcasing European furniture products, furniture collections, brand stories, and customisation services. This template helps high-end furniture brands showcase their products online and attract discerning customers. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | Eyou...
👁 44
(Adaptive Mobile Version) Smart Phone AI Robot Voice Software Website Template – 1012

(Adaptive Mobile Version) Smart Phone AI Robot Voice Software Website Template – 1012 Practical Collection pbootcms Template

PbootCMS website template – an AI-powered smartphone robot voice software solution compatible with both PC and WAP devices. Its cutting-edge design is ideal for showcasing AI voice products, smartphone robots, and technical solutions. This template helps technology companies present their innovative products online, attracting investors and customers. Template Overview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4...
👁 51
(PC+WAP) Mining Drilling Rig – Mining Equipment Website (PBootCMS Template) – Blue Marketing-oriented Mining Machinery and Equipment Website Template Download – 0076

(PC+WAP) Mining Drilling Rig – Mining Equipment Website (PBootCMS Template) – Blue Marketing-oriented Mining Machinery and Equipment Website Template Download – 0076 Practical Collection pbootcms Template

This PbootCMS template features a blue marketing style and is specifically designed for the mining drilling rig and mining equipment industry, supporting both PC and WAP devices. Its page design focuses on product showcasing and technical specifications, effectively attracting customers in the mining sector. It enables mining machinery and equipment companies to showcase their product portfolio online and generate targeted inquiries. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extract...
👁 58