PHP でファイルの BOM ヘッダーをバッチ検出して削除する方法

WBOY
リリース: 2016-07-25 08:53:18
オリジナル
807 人が閲覧しました
  1. if (isset($_GET['dir'])) { //设置文件目录
  2. $basedir = $_GET['dir'];
  3. } else {
  4. $basedir = ' .';
  5. }
  6. $auto = 1;
  7. checkdir($basedir);
  8. function checkdir($basedir)
  9. {
  10. if ($dh = opendir($basedir)) {
  11. while (($file = readdir($ dh)) !== false) {
  12. if ($file != '.' && $file != '..') {
  13. if (!is_dir($basedir . "/" . $file)) {
  14. echo "ファイル名: $basedir/$file " . checkBOM("$basedir/$file") 。 "
    ";
  15. } else {
  16. $dirname = $basedir . 「/」。 $file;
  17. checkdir($dirname);
  18. }
  19. }
  20. }
  21. Closedir($dh);
  22. }
  23. }
  24. function checkBOM($filename)
  25. {
  26. global $auto;
  27. $contents = file_get_contents($filename) ;
  28. $charset[1] = substr($contents, 0, 1);
  29. $charset[2] = substr($contents, 1, 1);
  30. $charset[3] = substr($contents, 2, 1 );
  31. if (ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191) {
  32. if ($auto == 1) ) {
  33. $rest = substr($contents, 3);
  34. rewrite($filename, $rest);
  35. return ("BOM が見つかり、自動的に削除されました。" );
  36. } else {
  37. return ("BOM が見つかりました。");
  38. }
  39. } else
  40. return ("BOM が見つかりません。");
  41. }
  42. 関数 rewrite($filename, $data)
  43. {
  44. $filenum = fopen($filename, "w");
  45. flock($filenum, LOCK_EX);
  46. fwrite($filenum, $data);
  47. fclose($filenum) ;
  48. }
  49. ?>
复制發



ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!