0

特定のディレクトリ内のすべてのファイルを一覧表示するディレクトリ一覧コードが 1 つあります。ファイル名に az と 0-9 以外の特定の文字を含むものがあります。「i」の上にドットがない小さな「i」のようなものです。ファイル名は「ClrmamePro Kullanımı English.mp4」です。「Kullanımı」と「English」を見てください。「i」と「ı」の違いがわかります。

問題は、ディレクトリのリストを作成すると、php が文字 "ı" を "i" に自動的に変換するため、名前の変更を実行するときにエラーが発生することです。

rename(E:/workspace/project/ClrmamePro Kullanimi English.mp4,
E:/workspace/project/movie_11.mp4) [<a href='function.rename'>function.rename</a>]: The system cannot find the file specified.

ファイル名を修正する正規表現がありますが、PHP が自動的に "ı" を "i" に変換するため、それをキャッチできません。

ディレクトリリストのコードは次のとおりです

function getDirectoryListing($directory) {
    // create an array to hold directory list
    $results = array();
    // create a handler for the directory
    $handler = opendir($directory);
    // open directory and walk through the filenames

    while ($file = readdir($handler)) {
        // if file isn't this directory or its parent, add it to the results
        if (strpos($file,'.') !== 0) {
            $results[] = $file;
        }
    }

    closedir($handler);
    // done!
    return $results;
}

echo '<pre>';
print_r(getDirectoryListing('movies'));
echo '</pre>';

o/pi は次のようになります。

Array
(
    [0] => ClrmamePro Kullanimi English.mp4
    [1] => Download Gears of War 3 - eSoftZone.webm
    [2] => Facebook_ Science and the Social Graph.MP4
)

インデックス 0 の最初のファイルを参照してください。ディレクトリ内の実際のファイル名は

ClrmamePro Kullanımı English.mp4
4

1 に答える 1

0

ディレクトリ内の各ファイルについてmovies、次のコード スニペットはファイル名とファイルが存在するかどうかを出力します。表示されるファイル名は、特殊文字を正しく表示するためにエンコードされます。

このencodeメソッドは、文字列内のすべての文字"$file "を HTML エンティティに変換します。このメソッドは、スタック オーバーフローの記事「PHP を使用してすべての文字を同等の html エンティティに変換する方法」のソリューションからわずかに変更されたバージョンです。記事にある解決策は私の PHP サーバーでは機能しなかったためprependAmpersandAndPound、関数の外に移動しました。

// https://stackoverflow.com/a/3005240/788324
function prependAmpersandAndPound($n) {
    return "&#$n;";
}
function encode($str) {
    $str = mb_convert_encoding($str , 'UTF-32', 'UTF-8');
    $t = unpack("N*", $str);
    $t = array_map(prependAmpersandAndPound, $t);
    return implode("", $t);
}

echo "<pre>\n";
$listing = getDirectoryListing('movies');
foreach ($listing as $file) {
    echo "\"" . encode($file) . "\" ";
    if (file_exists('movies/' . $file)) {
        echo "exists.\n";
    } else {
        echo "does not exist.\n";
    }
}
echo '</pre>';

セットアップを模倣すると、上記のコード スニペットは以下を出力します。

HTML ソース:

<pre>
"&#68;&#111;&#119;&#110;&#108;&#111;&#97;&#100;&#32;&#71;&#101;&#97;&#114;&#115;&#32;&#111;&#102;&#32;&#87;&#97;&#114;&#32;&#51;&#32;&#45;&#32;&#101;&#83;&#111;&#102;&#116;&#90;&#111;&#110;&#101;&#46;&#119;&#101;&#98;&#109;" exists.
"&#67;&#108;&#114;&#109;&#97;&#109;&#101;&#80;&#114;&#111;&#32;&#75;&#117;&#108;&#108;&#97;&#110;&#305;&#109;&#305;&#32;&#69;&#110;&#103;&#108;&#105;&#115;&#104;&#46;&#109;&#112;&#52;" exists.
"&#70;&#97;&#99;&#101;&#98;&#111;&#111;&#107;&#95;&#32;&#83;&#99;&#105;&#101;&#110;&#99;&#101;&#32;&#97;&#110;&#100;&#32;&#116;&#104;&#101;&#32;&#83;&#111;&#99;&#105;&#97;&#108;&#32;&#71;&#114;&#97;&#112;&#104;&#46;&#77;&#80;&#52;" exists.
</pre>

ブラウザに表示:

"Download Gears of War 3 - eSoftZone.webm" exists.
"ClrmamePro Kullanımı English.mp4" exists.
"Facebook_ Science and the Social Graph.MP4" exists.
于 2012-06-01T13:04:41.607 に答える