2012-03-10 15 views
0

ファイルのハウンダッドを含むPHPアプリケーションがあります。問題は、1つまたは複数のファイルに明らかにBOMがあるため、セッションを作成するときにエラーが発生する... PHPやサーバーを再構成する方法はありますか、どのようにしてBOMを取り除くことができますか?または、少なくともソースを特定しますか?私はvimの内部に一度バイトオーダーマークでセッションエラーが発生しました

+0

これらのファイルはどのように作成されましたか? –

+1

http://stackoverflow.com/questions/204765/elegant-way-to-search-for-utf-8-files-with-bom –

+0

はどのように作成されたか違いはありません... @anders lindahlこれはシェルスクリプトです、私はそれを使用することはできません... – Michal

答えて

0

私はこのスクリプトでBOMを含むファイルを識別できました。おそらく将来この同じ問題を持つ他の人に役立つかもしれません。作品なしのeval()

function fopen_utf8 ($filename) { 
    $file = @fopen($filename, "r"); 
    $bom = fread($file, 3); 
    if ($bom != b"\xEF\xBB\xBF") 
    { 
     return false; 
    } 
    else 
    { 
     return true; 
    } 
} 

function file_array($path, $exclude = ".|..|libraries", $recursive = true) { 
    $path = rtrim($path, "/") . "/"; 
    $folder_handle = opendir($path); 
    $exclude_array = explode("|", $exclude); 
    $result = array(); 
    while(false !== ($filename = readdir($folder_handle))) { 
     if(!in_array(strtolower($filename), $exclude_array)) { 
      if(is_dir($path . $filename . "/")) { 
       // Need to include full "path" or it's an infinite loop 
       if($recursive) $result[] = file_array($path . $filename . "/", $exclude, true); 
      } else { 
       if (fopen_utf8($path . $filename)) 
       { 
        //$result[] = $filename; 
        echo ($path . $filename . "<br>"); 
       } 
      } 
     } 
    } 
    return $result; 
} 

$files = file_array("."); 
-1
vim $(find . -name \*.php) 

あれば利用できるPHPソリューションを好むだろう:

もちろんの真の解決策は、ファイルを保存しないように(だけでなく、その他のチームメンバー)エディタの設定を修正することです
:argdo :set nobomb | :w 
1

UTFバイトオーダーマーク付き。ここで読む:https://stackoverflow.com/a/2558793/43959

この機能を使用して、別のPHPファイルを含める前にBOMを「透過的に」削除することができます。

注:ここでデモするeval()で厄介なことをするのではなく、あなたのエディタ/ファイルを修正することをお勧めします。

これは概念だけの証拠である:

bom_test.php:先頭にBOMと

<?php 
function bom_safe_include($file) { 
     $fd = fopen($file, "r"); 
     // read 3 bytes to detect BOM. file read pointer is now behind BOM 
     $possible_bom = fread($fd, 3); 
     // if the file has no BOM, reset pointer to beginning file (0) 
     if ($possible_bom !== "\xEF\xBB\xBF") { 
       fseek($fd, 0); 
     } 
     $content = stream_get_contents($fd); 
     fclose($fd); 
     // execute (partial) script (without BOM) using eval 
     eval ("?>$content"); 
     // export global vars 
     $GLOBALS += get_defined_vars(); 
} 
// include a file 
bom_safe_include("test_include.php"); 
// test function and variable from include 
test_function($test); 

test_include.php、

test 
<?php 
$test = "Hello World!"; 
function test_function ($text) { 
     echo $text, PHP_EOL; 
} 

OUTPUT:

[email protected]$ php bom_test.php 
test 
Hello World! 
+0

しようとします...しかし、私のエディタが正しく設定されている、誰かがelsesではなかった.... – Michal

関連する問題