Hongmu Notes
Home Language Notes PHP: Deletes files with a specified suffix that are smaller than 300 KB, as well as empty files, from a specified directory.
Language Notes PHP

PHP: Deletes files with a specified suffix that are smaller than 300 KB, as well as empty files, from a specified directory.

PHP: Deletes files with a specified suffix that are smaller than 300 KB, as well as empty files, from a specified directory.

The size can be customized, and the file path can also be specified.

<?php
$directory = "./Music score/up"; // 替换为你想要操作的目录路径
// 递归删除文件和目录
function deleteFiles($path) {
    if (is_dir($path)) {
        $files = glob(rtrim($path, '/') . '/*');
        if (count($files) === 0) {
            rmdir($path);
            echo "已删除目录:" . $path . "<br>";
        } else {
            foreach ($files as $file) {
                deleteFiles($file);
            }
            // 检查目录是否为空
            $files = glob(rtrim($path, '/') . '/*');
            if (count($files) === 0) {
                rmdir($path);
                echo "已删除目录:" . $path . "<br>";
            }
        }
    } elseif (is_file($path) && filesize($path) < 100 * 1024) {
        unlink($path);
        echo "已删除文件:" . $path . "<br>";
    }
}

// 调用删除函数
deleteFiles($directory);

Second Version

<?php
function deleteFiles($dir, $extensions, $maxSize) {
    if (!is_dir($dir)) {
        return;
    }

    $files = scandir($dir);

    foreach ($files as $file) {
        if ($file == '.' || $file == '..') {
            continue;
        }

        $path = $dir . '/' . $file;

        if (is_dir($path)) {
            deleteFiles($path, $extensions, $maxSize);
        } else {
            $extension = pathinfo($path, PATHINFO_EXTENSION);

            if (in_array($extension, $extensions) && filesize($path) < $maxSize) {
                unlink($path);
                echo "已删除文件:".$path."<br>";
            } elseif (filesize($path) == 0) {
                unlink($path);
                echo "已删除文件:".$path."<br>";
            }
        }
    }
}

$directory = '/path/to/directory'; // 替换为目标目录的路径
$allowedExtensions = ['txt', 'jpg', 'png']; // 替换为要删除的文件后缀列表
$maxFileSize = 300 * 1024; // 300KB,单位为字节

deleteFiles($directory, $allowedExtensions, $maxFileSize);
?>

 

微信赞赏

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

Fashion Designer – Clothing Manufacturer Website Template 0238

Fashion Designer – Clothing Manufacturer Website Template 0238 Practical Collection Yiyou template

An eYouCMS website template designed specifically for the fashion design and garment manufacturing industry. Its stylish and professional design enables effective presentation of fashion design works, product collections, production processes, and brand images. This template helps apparel businesses showcase their products online and attract both wholesale and retail customers. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for eYouCMS...
👁 39
(Adaptive mobile version) Responsive cake and dessert website template – PBootCMS; Download HTML5 dessert and pastry website source code – 0763

(Adaptive mobile version) Responsive cake and dessert website template – PBootCMS; Download HTML5 dessert and pastry website source code – 0763 Practical Collection pbootcms Template

A responsive PbootCMS website template for cakes, desserts, and sweet treats, compatible with both PC and WAP devices. Its sweet and appealing design makes it ideal for cake shops and dessert brands to showcase their product ranges and brand stories. This template helps baking brands attract customers online and enhance their brand awareness. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 63
Home Textiles – Knitted Bedding Website Template 0910

Home Textiles – Knitted Bedding Website Template 0910 Practical Collection Yiyou template

This EyouCMS template is ideal for the home textiles, knitwear, and bedding industries. Its warm and cozy design style is perfect for showcasing home textile products, bedding collections, brand stories, and home decor combinations. It helps home textile brands showcase their products online and attract household consumers. Template Demo | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 31
(PC + WAP) Freight Logistics & Express Delivery – pbootCMS Website Template; Warehouse Racking System Website Source Code Download – 0121

(PC + WAP) Freight Logistics & Express Delivery – pbootCMS Website Template; Warehouse Racking System Website Source Code Download – 0121 Practical Collection pbootcms Template

This PbootCMS template is ideal for websites specializing in freight logistics, express delivery services, or warehouse shelving solutions, and it supports both PC and WAP devices. Its clear and practical design effectively showcases warehouse management solutions and logistics delivery capabilities. It helps warehousing and logistics enterprises demonstrate their online service offerings and expand their customer base across e-commerce and manufacturing sectors. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www...
👁 51
Responsive Business Training and Education Website Template 0239

Responsive Business Training and Education Website Template 0239 Practical Collection Yiyou template

This eyouCMS responsive template is ideal for the business training and education sectors, featuring a professional design style that effectively showcases business training courses, faculty expertise, teaching outcomes, and training services. It helps business training institutions attract corporate learners online. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS (Ey...
👁 37