共計 593 個字符,預(yù)計需要花費(fèi) 2 分鐘才能閱讀完成。
可以使用 PHP 的 readdir()
函數(shù)和 rename()
函數(shù)來批量修改文件名。
首先,使用 readdir()
函數(shù)讀取目標(biāo)文件夾中的所有文件名,將文件名存儲在一個數(shù)組中。然后,使用循環(huán)遍歷數(shù)組中的每個文件名,并使用 rename()
函數(shù)來修改文件名。
以下是一個示例代碼:
$dir = 'path/to/directory/'; // 目標(biāo)文件夾路徑
if (is_dir($dir)) {if ($dh = opendir($dir)) {while (($file = readdir($dh)) !== false) {if ($file != '.' && $file != '..') {
// 修改文件名邏輯
$newName = 'new_file_name'; // 新的文件名
$newFilePath = $dir . $newName;
$oldFilePath = $dir . $file;
rename($oldFilePath, $newFilePath);
echo "文件名已修改:{$file} -> {$newName}";
}
}
closedir($dh);
}
}
請注意,在上述示例代碼中,將 new_file_name
替換為新的文件名,以及將 path/to/directory/
替換為目標(biāo)文件夾的實(shí)際路徑。
此代碼將目標(biāo)文件夾中的每個文件名修改為new_file_name
。在循環(huán)中,可以根據(jù)需求定義不同的新文件名生成邏輯。
丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!
正文完