Hongmu Notes
Home Language Notes Move tens of millions of files with specified extensions using PHP.
Language Notes PHP

Move tens of millions of files with specified extensions using PHP.

Move tens of millions of files with specified extensions using PHP.

Got it! You want to write a function that takes three parameters – the source folder path, the target folder path, and the file extension – and moves files with that specific extension from the source folder to the target folder. Here's an example of the code:

function moveFilesByExtension($sourceDir, $targetDir, $fileExtension) {
    if (!is_dir($sourceDir) || !is_dir($targetDir)) {
        echo '源文件夹或目标文件夹不存在';
    } else {
        if ($dh = opendir($sourceDir)) {
            while (($file = readdir($dh)) !== false) {
                if (substr($file, -strlen($fileExtension)) === $fileExtension) {
                    rename("$sourceDir/$file", "$targetDir/$file");
                }
            }
            closedir($dh);
        }
    }
}

// 调用函数
moveFilesByExtension('./回收站', './句子采集', '.txt');

In this example, I define an object namedmoveFilesByExtensionThis function accepts three parameters: the source folder path, the destination folder path, and the file extension. Within the function, I employ logic similar to that used previously to perform the file relocation operation.

You can call this function according to your specific needs and pass in the appropriate parameters. We hope this meets your requirements; if you have any further questions, feel free to ask!

微信赞赏

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

(PC + WAP) Green Silicone Rubber Products – pbootCMS Website Template; Download Marketing Toy Products Website Source Code – 0090

(PC + WAP) Green Silicone Rubber Products – pbootCMS Website Template; Download Marketing Toy Products Website Source Code – 0090 Practical Collection pbootcms Template

This PbootCMS template is designed for the green silicone and rubber products as well as toy manufacturing industries. It adopts a marketing-oriented design approach and supports both PC and WAP devices. The page design focuses on product showcasing and brand identity, helping rubber or toy manufacturers showcase their product diversity online, enhance brand awareness, and increase sales conversion rates. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extract...
👁 47
Adult Self-Study Education Promotion Landing Page – Single-Page Website Template 0848

Adult Self-Study Education Promotion Landing Page – Single-Page Website Template 0848 Practical Collection Yiyou template

An EyouCMS single-page website template designed for promoting adult self-study education programs. This template features a strong marketing-oriented design with a compact, efficient layout that effectively highlights course advantages, faculty expertise, and enrollment portals. It serves as an excellent tool for educational institutions conducting SEM campaigns and rapidly acquiring new customers. Template Preview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Common Installation Questions for EyouCMS...
👁 36
Internet Information Services Website Template 0188

Internet Information Services Website Template 0188 Practical Collection Yiyou template

An EyouCMS website template designed for internet-based information technology service providers. Its modern, tech-driven design effectively showcases IT solutions, technical services, product portfolios, and corporate strength. This template helps IT service companies present their brand online and attract business clients. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS...
👁 30
(PC+WAP) New Energy Vehicle Charging Station Website – pbootCMS Template; Download Source Code for Automotive Charging Station Websites – 0723

(PC+WAP) New Energy Vehicle Charging Station Website – pbootCMS Template; Download Source Code for Automotive Charging Station Websites – 0723 Practical Collection pbootcms Template

A PbootCMS website template designed for the new energy vehicle charging station and supporting infrastructure industry, compatible with both PC and mobile devices. Its modern, tech-driven design effectively showcases charging station products and smart charging solutions. This template is an excellent tool for new energy technology companies to build official brand websites and promote their green mobility initiatives. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password...
👁 62
(PC + WAP) Green Turf Flooring Installation – pbootCMS Website Template; Playground Artificial Turf Website Source Code Download – 0091

(PC + WAP) Green Turf Flooring Installation – pbootCMS Website Template; Playground Artificial Turf Website Source Code Download – 0091 Practical Collection pbootcms Template

A PbootCMS website template designed for green lawns, flooring installation projects, and artificial turf for sports fields, compatible with both PC and WAP devices. Its fresh, natural design effectively showcases lawn installation case studies and construction techniques. This template is ideal for lawn installation companies or sports facility development enterprises to attract online customers and promote their brand. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password:...
👁 34
Responsive Business Training and Education Website Template 0849

Responsive Business Training and Education Website Template 0849 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...
👁 39