Created
August 11, 2019 14:40
-
-
Save naknaknakjp/960fdbd483918f542677617ae680d372 to your computer and use it in GitHub Desktop.
PHPでディレクトリ内のファイル名一括変換
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// ディレクトリパス | |
$dir = '/path/to/directory'; | |
// ファイルリスト取得 | |
foreach(glob($dir . '*') as $filepath) { | |
// 1件ずつファイルを処理 | |
if(is_file($filepath)) { | |
// ファイル名取得 | |
$filename = basename($filepath); | |
// 新しいファイル名生成 | |
// ここでは例として拡張子を.txtから.phpに変更する | |
$newFilename = str_replace('.txt', '.php', $filename); | |
// ファイル名をリネーム処理 | |
rename($filepath, $dir . $newFilename); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment