Skip to content

Instantly share code, notes, and snippets.

@brycefisher
Created September 19, 2013 17:15
Show Gist options
  • Select an option

  • Save brycefisher/6626671 to your computer and use it in GitHub Desktop.

Select an option

Save brycefisher/6626671 to your computer and use it in GitHub Desktop.
Scan a directory for NON utf8 files using PHP
<?php
// Note this is NOT recursive.
function scan_dir_for_non_utf8($dir_path) {
if ($dir = opendir($dir_path)) {
echo "opening $dir_path";
$files_scanned = 0;
while (($file = readdir($dir)) !== false) {
// Don't go up directory levels
if (in_array($file, array('.','..')))
continue;
if (filetype($dir_path.$file) === 'file') {
$content = file_get_contents($file);
if (mb_detect_encoding($content, 'UTF-8', true) == 'UTF-8')
$file_scanned++;
else
exit("$file NOT utf8\n\n");
}
}
closedir($dir);
}
echo "\nscanned $file_scanned files. all utf8\n";
}
scan_dir_for_non_utf8( __DIR__ );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment