Skip to content

Instantly share code, notes, and snippets.

@halilim
Created September 1, 2013 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halilim/6405368 to your computer and use it in GitHub Desktop.
Save halilim/6405368 to your computer and use it in GitHub Desktop.
Mass convert encodings of files with selected extensions in a folder recursively.
#!/usr/bin/php
<?php
if (count($argv) < 6) {
echo '
Usage :
> php ' . $_SERVER['SCRIPT_FILENAME'] . '
source_folder
destination_folder
source_charset
destination_charset
file_types
Example:
php ' . $_SERVER['SCRIPT_FILENAME'] . '
"etc/something"
"etc/something_utf8"
"iso-8859-9"
"utf-8"
"php, php3, inc, phtml, html, htm, js"
';
exit;
} else {
$source_folder_i = trim($argv[1]);
$destination_folder_i = trim($argv[2]);
$source_charset_i = trim($argv[3]);
$destination_charset_i = trim($argv[4]);
$file_types_i = trim($argv[5]);
$file_types_i = explode(',', $file_types_i);
foreach ($file_types_i as $k => $v) {
$file_types_i[$k] = trim($v);
if ($file_types_i[$k] == '') {
unset($file_types_i[$k]);
}
}
if (count($file_types_i) < 1) {
echo 'Specify at least one file type.';
exit;
}
/*
var_dump($source_folder_i);echo "\r\n";
var_dump($destination_folder_i);echo "\r\n";
var_dump($source_charset_i);echo "\r\n";
var_dump($destination_charset_i);echo "\r\n";
var_dump($file_types_i);echo "\r\n";
exit;
*/
$result = process_folder(
$source_folder_i,
$destination_folder_i,
$source_charset_i,
$destination_charset_i,
$file_types_i
);
if ($result['result']) {
echo PHP_EOL . 'OK.' . PHP_EOL;
} else {
echo PHP_EOL . 'ERROR OCCURRED : ' . $result['msg'] . PHP_EOL;
}
function process_folder(
$source_folder,
$destination_folder,
$source_charset,
$destination_charset,
$file_types
) {
$source_folder = rtrim($source_folder, DIRECTORY_SEPARATOR);
$source_folder .= DIRECTORY_SEPARATOR;
$destination_folder = rtrim($destination_folder, DIRECTORY_SEPARATOR);
$destination_folder .= DIRECTORY_SEPARATOR;
if (!is_dir($destination_folder)) {
if (!mkdir($destination_folder)) {
return array(
'result' => false,
'msg' => $destination_folder . ' cannot be created.'
);
}
}
if (is_dir($source_folder)) {
if ($dh = opendir($source_folder)) {
while (($file = readdir($dh)) !== false) {
$path = $source_folder . $file;
if ($file == '.' || $file == '..') {
continue;
}
if (is_dir($path)) {
process_folder(
$path,
$destination_folder . $file,
$source_charset,
$destination_charset,
$file_types
);
} else {
$type = pathinfo($path, PATHINFO_EXTENSION);
if ($type !== false) {
if (in_array($type, $file_types, true)) {
$content = file_get_contents($path);
if ($content !== false) {
$content = mb_convert_encoding(
$content,
$destination_charset,
$source_charset
);
if ($content !== false) {
if (file_put_contents(
$destination_folder . $file,
$content
)
) {
echo $path
. ' (' . filesize($path) . ' B)'
. ' is OK.' . PHP_EOL;
} else {
return array(
'result' => false,
'msg' => 'file_put_contents problem.'
);
}
} else {
return array(
'result' => false,
'msg' => 'mb_convert_encoding problem.'
);
}
} else {
return array(
'result' => false,
'msg' => 'file_get_contents problem.'
);
}
} else {
if (copy($path, $destination_folder . $file)) {
echo $path . ' copied as-is.' . PHP_EOL;
} else {
return array(
'result' => false,
'msg' => $path . ' cannot be copied to '
. $destination_folder . $file . '.'
);
}
}
} else {
return array(
'result' => false,
'msg' => 'pathinfo problem.'
);
}
}
}
closedir($dh);
} else {
return array('result' => false, 'msg' => 'opendir problem.');
}
} else {
return array(
'result' => false,
'msg' => $source_folder . ' is not a folder.'
);
}
return array('result' => true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment