[PHP]カレントディレクトリとサブディレクトリのファイルのエンコーディングを変更してくれるPHP Script
マックでテキストファイルをフォルダ目にEncoding変換しようとか探すのが大変だった。何かShell Scriptはあるが、私は慣れて言わないしない。これだけphpスクリプトを作った。サーバーに置いて、Webブラウザでアクセスするとされるようにした。たぶんシェルで実行させてもされたりするだろうしてみはしなかった。
使い方は以下の通りです。
iconv_all_file('.', 'euc-kr','utf-8', true);
関数は以下の通りです。
function iconv_all_file($dir, $input, $output, $subdir = false){
echo "既存のファイルを上書きします。<br>";
$ext_arr = array('txt','php','html','htm','js','css','java','jsp','asp','md');
$dir = realpath(trim($dir));
echo "{$dir}のファイルの変換を開始します。<br>";
if ($handle = opendir($dir)) {
while (false !== ($entry = readdir($handle))) {
if($entry == '.' || $entry == '..'){
continue;
}
$fullpath = $dir . '/' . $entry;
//変換
if(is_file($fullpath)){
//現在のファイルであればスキッ
if($fullpath == __FILE__){
continue;
}
//指定された拡張子がない場合が開く。
$ext = strtolower(pathinfo($fullpath, PATHINFO_EXTENSION));
if( ! in_array($ext, $ext_arr) ){
echo "{$fullpath} : 対象の拡張子がないので、変換せずに進みます。<br>";
continue;
}
$content = file_get_contents($fullpath);
$content = iconv($input,$output,$content);
$fp = fopen($fullpath, 'w');
$result = fwrite($fp, $content);
fclose($fp);
if($result >= 0){
echo "<div style='color:green'>{$fullpath} 変換完了。({$result}bytes)</div>";
}else{
echo "<div style='color:red'>{$fullpath} 変換に失敗。</div>";
}
}else if($subdir == true AND is_dir($fullpath)){
//サブディレクトリの処理
iconv_all_file($fullpath, $input, $output);
}
}
closedir($handle);
}else{
echo "opendir 失敗<br>";
}
}
- コメント機能はありません。コメントの代わりに[email protected]にメールを送ってください。