遞迴掃描指定目錄及子目錄,將所有檔案副檔名統一修改為 .JPG(大寫)。
<?php
function foreachDir($path) {
$handle = opendir($path);
if ($handle) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != '..') {
$fullPath = $path . '/' . $file;
if (is_dir($fullPath)) {
echo $fullPath . "<br/>";
foreachDir($fullPath);
} else {
echo "--" . $fullPath . "<br/>";
$ext = strripos($file, '.');
$name = substr($file, 0, $ext);
rename($fullPath, $path . '/' . $name . '.JPG');
}
}
}
closedir($handle);
}
return false;
}
foreachDir('D:\\xampp\\htdocs\\TNF2');
?>
strripos 找最後一個點,支援多點檔名。rename 為不可逆操作。die() 已註解,正式使用時可移除。