{"id":340,"date":"2014-07-03T02:40:50","date_gmt":"2014-07-03T02:40:50","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=340"},"modified":"2014-07-03T02:40:50","modified_gmt":"2014-07-03T02:40:50","slug":"php%e6%a3%80%e6%b5%8b%e6%96%87%e4%bb%b6%e7%9a%84bom%e5%a4%b4bak","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/340.html","title":{"rendered":"PHP\u68c0\u6d4b\u6587\u4ef6\u7684BOM\u5934[bak]"},"content":{"rendered":"<p>\u68c0\u6d4b\u6587\u4ef6\u662f\u5426\u4e3aUTF-8\u5e26BOM\u683c\u5f0f\u7684\u65b9\u6cd5\u5176\u5b9e\u5c31\u662f\u53d6\u6587\u4ef6\u7684\u524d3\u4e2a\u5b57\u7b26\u8fdb\u884c\u68c0\u6d4b\u5373\u53ef\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:php decode:true \">&lt;?php\n$auto = 0; \/\/\u4e0d\u81ea\u52a8\u5220\u9664\u6587\u4ef6\u4e2d\u7684BOM\u5934\nfunction checkBOM ($filename) {\n\tglobal $auto;\n\t$contents = file_get_contents($filename);\n\t$charset[1] = substr($contents, 0, 1);\n\t$charset[2] = substr($contents, 1, 1);\n\t$charset[3] = substr($contents, 2, 1);\n\tif (ord($charset[1]) == 239 &amp;&amp; ord($charset[2]) == 187 &amp;&amp; ord($charset[3]) == 191) {\n\t\tif ($auto == 1) {\n\t\t\t$rest = substr($contents, 3);\n\t\t\trewrite ($filename, $rest);\n\t\t\treturn (\"&lt;font color=red&gt;BOM found, automatically removed.&lt;\/font&gt;\");\n\t\t} else {\n\t\t\treturn (\"&lt;font color=red&gt;BOM found.&lt;\/font&gt;\");\n\t\t}\n\t}\n\telse return (\"BOM Not Found.\");\n}\nfunction rewrite($filename, $rest) {\n\tfseek($filename, 0);\t\/\/\u8df3\u8f6c\u56de\u6587\u4ef6\u7684\u5f00\u5934\n\tfile_put_contents($filename, $rest); \/\/\u5c06$rest\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\u5199\u5165\u6587\u4ef6$filename\n}\n\necho checkBOM(\"D:withBOM.txt\");<\/pre>\n<p>\u7136\u540e\uff0c\u6dfb\u52a0\u4e00\u4e2a\u904d\u5386\u76ee\u5f55\u7684\u51fd\u6570\u5373\u53ef\u8fdb\u884c\u6279\u91cf\u68c0\u6d4b\/\u5220\u9664\uff1a<\/p>\n<pre class=\"lang:php decode:true\">&lt;?php\nif (isset($_GET['dir'])){ \/\/\u8bbe\u7f6e\u6587\u4ef6\u76ee\u5f55\n\t$basedir = $_GET['dir'];\n}else{\n\t$basedir = '.';\n}\n$auto = 0;\nlist_dir($basedir);\nfunction list_dir($basedir){\n\tif ($dh = opendir($basedir)) {\n\t\twhile (($file = readdir($dh)) !== false) {\n\t\t\tif ($file != '.' &amp;&amp; $file != '..'){\n\t\t\t\tif (!is_dir($basedir.\"\/\".$file)) {\n\t\t\t\t\techo \"filename: $basedir\/$file \".checkBOM(\"$basedir\/$file\").\" &lt;br&gt;\";\n\t\t\t\t}else{\n\t\t\t\t\t$dirname = $basedir.\"\/\".$file;\n\t\t\t\t\tlist_dir($dirname);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tclosedir($dh);\n\t}\n}\nfunction checkBOM ($filename) {\n\tglobal $auto;\n\t$contents = file_get_contents($filename);\n\t$charset[1] = substr($contents, 0, 1);\n\t$charset[2] = substr($contents, 1, 1);\n\t$charset[3] = substr($contents, 2, 1);\n\tif (ord($charset[1]) == 239 &amp;&amp; ord($charset[2]) == 187 &amp;&amp; ord($charset[3]) == 191) {\n\t\tif ($auto == 1) {\n\t\t\t$rest = substr($contents, 3);\n\t\t\trewrite ($filename, $rest);\n\t\t\treturn (\"&lt;font color=red&gt;BOM found, automatically removed.&lt;\/font&gt;\");\n\t\t} else {\n\t\t\treturn (\"&lt;font color=red&gt;BOM found.&lt;\/font&gt;\");\n\t\t}\n\t}\n\telse return (\"BOM Not Found.\");\n}\nfunction rewrite($filename, $data) {\n\t$filenum = fopen($filename, \"w\");\n\tflock($filenum, LOCK_EX);\n\tfwrite($filenum, $data);\n\tfclose($filenum);\n}\n?&gt;<\/pre>\n<p>\u4ee3\u7801\u6536\u96c6\uff0c\u65b9\u4fbf\u81ea\u5df1\u968f\u65f6\u67e5\u770b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u68c0\u6d4b\u6587\u4ef6\u662f\u5426\u4e3aUTF-8\u5e26BOM\u683c\u5f0f\u7684\u65b9\u6cd5\u5176\u5b9e\u5c31\u662f\u53d6\u6587\u4ef6\u7684\u524d3\u4e2a\u5b57\u7b26\u8fdb\u884c\u68c0\u6d4b\u5373\u53ef\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a &lt;?php [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,12],"tags":[160,48],"class_list":["post-340","post","type-post","status-publish","format-standard","hentry","category-programing","category-tools","tag-bom","tag-php"],"views":1987,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/340","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=340"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/340\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=340"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}