Created
September 19, 2013 17:15
-
-
Save brycefisher/6626671 to your computer and use it in GitHub Desktop.
Scan a directory for NON utf8 files using 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 | |
| // 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