
好用,爱用。
<?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 " ";
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>";
?>
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END


![表情[aini]-红穆笔记](https://www.4s5.cn/wp-content/themes/zibll/img/smilies/aini.gif)
![表情[ciya]-红穆笔记](https://www.4s5.cn/wp-content/themes/zibll/img/smilies/ciya.gif)
![表情[xia]-红穆笔记](https://www.4s5.cn/wp-content/themes/zibll/img/smilies/xia.gif)


暂无评论内容