Hongmu Notes
Home Language Notes Batch rename HTML files to PHP files
Language Notes PHP

Batch rename HTML files to PHP files

Batch rename HTML files to PHP files

20251209124209281-image

Easy to use – love using it!

<?php
// 安全提示:建议在测试环境中先运行,备份重要文件
echo "<h3>HTML文件批量重命名为PHP</h3>";

// 检查是否有确认参数
if (isset($_GET['confirm']) && $_GET['confirm'] == 'yes') {
    renameHtmlToPhp();
} else {
    showWarning();
}

function showWarning() {
    echo "<div style='background-color:#fff3cd; border:1px solid #ffeaa7; padding:15px; margin:20px; border-radius:5px;'>";
    echo "<h4>⚠️ 安全警告</h4>";
    echo "<p>此脚本将执行以下操作:</p>";
    echo "<ol>";
    echo "<li>递归扫描当前目录及其所有子目录</li>";
    echo "<li>查找所有 .html 文件</li>";
    echo "<li>将它们重命名为 .php 文件</li>";
    echo "</ol>";
    echo "<p style='color:#d63031; font-weight:bold;'>注意:此操作不可逆,请确保已备份重要文件!</p>";
    echo "<br>";
    echo "<a href='?confirm=yes' style='background-color:#e74c3c; color:white; padding:10px 20px; text-decoration:none; border-radius:4px;'>确认执行重命名</a>";
    echo "&nbsp;&nbsp;";
    echo "<a href='?' style='background-color:#95a5a6; color:white; padding:10px 20px; text-decoration:none; border-radius:4px;'>取消</a>";
    echo "</div>";
}

function renameHtmlToPhp() {
    $baseDir = __DIR__; // 当前目录
    $count = 0;
    $errors = 0;
    
    echo "<div style='background-color:#d4edda; border:1px solid #c3e6cb; padding:15px; margin:20px; border-radius:5px;'>";
    echo "<h4>正在执行重命名操作...</h4>";
    
    // 使用递归迭代器遍历所有目录
    $iterator = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($baseDir, RecursiveDirectoryIterator::SKIP_DOTS),
        RecursiveIteratorIterator::SELF_FIRST
    );
    
    foreach ($iterator as $file) {
        // 检查是否是.html文件
        if ($file->isFile() && strtolower($file->getExtension()) === 'html') {
            $oldPath = $file->getPathname();
            $newPath = $file->getPath() . DIRECTORY_SEPARATOR . $file->getBasename('.html') . '.php';
            
            echo "<p>处理文件: " . htmlspecialchars($oldPath) . "</p>";
            
            // 检查目标文件是否已存在
            if (file_exists($newPath)) {
                echo "<p style='color:#e67e22;'>警告: " . htmlspecialchars($newPath) . " 已存在,跳过</p>";
                $errors++;
                continue;
            }
            
            // 重命名文件
            if (rename($oldPath, $newPath)) {
                echo "<p style='color:#27ae60;'>成功重命名为: " . htmlspecialchars($newPath) . "</p>";
                $count++;
            } else {
                echo "<p style='color:#c0392b;'>错误: 无法重命名 " . htmlspecialchars($oldPath) . "</p>";
                $errors++;
            }
        }
    }
    
    echo "<hr>";
    echo "<h4>操作完成!</h4>";
    echo "<p>成功重命名: <strong>{$count}</strong> 个文件</p>";
    echo "<p>遇到错误: <strong>{$errors}</strong> 个文件</p>";
    echo "<p><a href='?'>返回</a></p>";
    echo "</div>";
}

// 显示当前目录信息
echo "<div style='background-color:#e9ecef; padding:10px; margin:10px 0; border-radius:4px;'>";
echo "<p><strong>当前目录:</strong> " . htmlspecialchars(__DIR__) . "</p>";
echo "</div>";
?>

 

微信赞赏

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

Responsive English-language general-purpose e-commerce website template – 1079

Responsive English-language general-purpose e-commerce website template – 1079 Practical Collection Yiyou template

An EyouCMS responsive website template designed for English-speaking foreign trade enterprises. Its international, minimalist design makes it ideal for export companies to create official English-language showcase websites, helping Chinese manufacturing enterprises showcase their brand image and product capabilities in the global market. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS (E...
👁 50
Responsive Water Supply Pipeline Valve Website Template 0475

Responsive Water Supply Pipeline Valve Website Template 0475 Practical Collection Yiyou template

An eYouCMS responsive website template designed for the water supply pipeline and valve industry. Its professional industrial design style is ideal for showcasing pipeline products, valve series, technical specifications, and engineering applications. This template helps pipeline and valve manufacturers showcase their products online and attract construction and industrial clients. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for eYouCMS...
👁 68
(PC+WAP) Green Water Treatment Equipment Website Template – Pure Water Equipment Website Source Code Download (1064)

(PC+WAP) Green Water Treatment Equipment Website Template – Pure Water Equipment Website Source Code Download (1064) Practical Collection pbootcms Template

A green water treatment equipment and pure water system website template (PbootCMS), compatible with both PC and WAP devices. Featuring a fresh, tech-inspired design, this template is ideal for showcasing water treatment equipment, pure water systems, and project cases. It helps environmental water treatment companies demonstrate their capabilities online and attract customers. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 40
(Adaptive mobile version) Black Landscape Photography Studio website – PBootCMS template; Personal portrait photography website source code download – 0494

(Adaptive mobile version) Black Landscape Photography Studio website – PBootCMS template; Personal portrait photography website source code download – 0494 Practical Collection pbootcms Template

A black-themed landscape photography studio and personal portrait photography PbootCMS website template, compatible with both PC and WAP devices. Featuring a cool, artistic design, this template is ideal for photographers and photography studios to showcase their portfolios and services. It helps photography businesses attract high-end clients online and enhance their brand image. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5...
👁 67
Apple cms calls related articles of the current column

Apple cms calls related articles of the current column Program Notes Apple cms

Calling the video under the current column in Apple CMS can be achieved through the type="current" parameter of the template tag {maccms:vod}. The following are the specific steps and extended configuration instructions: {maccms:vod num="12" type="current" order="desc" by="hits"} <li class="clea…
👁 217
Responsive Mechanical Equipment Foreign Trade Enterprise Website Template (1080p)

Responsive Mechanical Equipment Foreign Trade Enterprise Website Template (1080p) Practical Collection Yiyou template

This EyouCMS responsive template is ideal for machinery and equipment trading enterprises operating in the international market. Its professional, global design style effectively showcases machinery products, technical specifications, export advantages, and global market presence. It helps machinery companies enhance their brand visibility and secure overseas orders in the international market. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 45