Skip to content

Instantly share code, notes, and snippets.

@naknaknakjp
Created August 11, 2019 14:40
Show Gist options
  • Save naknaknakjp/960fdbd483918f542677617ae680d372 to your computer and use it in GitHub Desktop.
Save naknaknakjp/960fdbd483918f542677617ae680d372 to your computer and use it in GitHub Desktop.
PHPでディレクトリ内のファイル名一括変換
<?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