Hongmu Notes
Home Language Notes PHP – Delete all files and directories in the current directory.
Language Notes PHP

PHP – Delete all files and directories in the current directory.

PHP – Delete all files and directories in the current directory.

This function – hehehe – I hope none of you have any ill intentions!

<?php
function deleteFiles($dir) {
    $files = glob("$dir/*"); // 查找当前目录下的所有文件和目录
    foreach($files as $file) {
        if(is_file($file)) {
            unlink($file); // 删除不需要的文件
        } elseif(is_dir($file)) {
            deleteFiles($file); // 递归处理子目录
        }
    }
    rmdir($dir); // 删除当前目录
}

deleteFiles("."); // 删除当前目录下的所有文件和目录

?>

Secondary demand

When running this PHP script, I want to keep two files – for example, 1.php and 2.php!

You can write it like this:

function deleteFiles($dir) {
    // 获取当前目录下的所有文件和目录
    $files = glob("$dir/*");
    
    // 遍历所有文件和目录
    foreach($files as $file) {
        // 如果是文件并且不是1.php和2.php,就删除
        if(is_file($file) && $file != "$dir/1.php" && $file != "$dir/2.php") {
            unlink($file);
        } 
        // 如果是目录,递归处理子目录
        elseif(is_dir($file)) {
            deleteFiles($file);
            // 删除空目录
            rmdir($file);
        }
    }
}

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

Please note: before running this code, it is recommended that you back up your current directory to prevent accidental deletion of important files.

微信赞赏

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) SEO Optimization and Ranking Service Company Website Template – 1006

(Adaptive Mobile Version) SEO Optimization and Ranking Service Company Website Template – 1006 Practical Collection pbootcms Template

A PbootCMS website template designed for SEO optimization and ranking services, compatible with both PC and WAP devices. Its modern, tech-driven design makes it ideal for SEO service providers and digital marketing agencies to showcase case studies and optimization results. It helps internet marketing firms attract corporate clients seeking promotional campaigns online. Template Overview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4...
👁 63
Steel Structure Outpost Website Template 0807

Steel Structure Outpost Website Template 0807 Practical Collection Yiyou template

This EyouCMS template is designed for the steel structure booth industry, featuring a professional industrial design style that effectively showcases steel structure booth products, project cases, and customized services. It enables steel structure enterprises to present their products online and attract both municipal and commercial clients. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS (Eyou...
👁 49
(PC + WAP) E-commerce Information Consulting and News Website – pbootCMS Template; Download Source Code for E-commerce Information Websites – 0072

(PC + WAP) E-commerce Information Consulting and News Website – pbootCMS Template; Download Source Code for E-commerce Information Websites – 0072 Practical Collection pbootcms Template

This PbootCMS template is ideal for e-commerce information consultation and news websites, supporting both PC and mobile access. Its modern and clean design effectively aggregates the latest e-commerce industry trends, policy interpretations, and practical operational insights. It serves as an efficient solution for building e-commerce industry portals or vertical news platforms. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: w...
👁 41
(Adaptive Mobile Version) HTML5 Responsive Formaldehyde Detection Website Template – 1007

(Adaptive Mobile Version) HTML5 Responsive Formaldehyde Detection Website Template – 1007 Practical Collection pbootcms Template

An HTML5 responsive formaldehyde detection PbootCMS website template compatible with both PC and WAP devices. Featuring a professional, tech-oriented design ideal for formaldehyde detection and air purification service providers to showcase their services and case studies. This template helps environmental protection service companies attract both residential and corporate clients online. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 47
Empire CMS – Download and Promotional Adaptive Dual-Platform Template for the game "Imitating CoolGameNet" – Version 016

Empire CMS – Download and Promotional Adaptive Dual-Platform Template for the game "Imitating CoolGameNet" – Version 016 Practical Collection empire template

I paid a bit of money to obtain this template – it is available for both mobile and PC devices. However, due to the large volume of data (which was stored on Baidu Netdisk), I cleared the existing data and then simply added my own data after downloading the template. Additionally, since the corresponding pseudo-static rule wasn't found for this template online, I adapted the provided IIS rule into an Nginx rule – a task that required some careful effort. Example: Pseudo-static rule 🔒 Hidden content: View after commenting – `rewrite ^([^\....`
👁 349
Tent and Pavilion Construction Material Custom Design Website Template – 0808

Tent and Pavilion Construction Material Custom Design Website Template – 0808 Practical Collection Yiyou template

An eYouCMS website template designed for the tent, temporary shelter, construction materials, and custom design industries. Its professional and practical design style is ideal for showcasing tent and temporary shelter products, custom design solutions, and project cases. This template helps temporary shelter companies showcase their products online and attract clients in the event and construction sectors. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Common Installation Questions for eYouCMS...
👁 43