以前、この問題の調査を開始しました ここ。真の問題と提案された解決策は次のとおりです。
32 ~ 255 の ASCII 文字値を持つファイル名は、utf8_encode() で問題を引き起こします。具体的には、126 から 160 までの文字値を正しく処理しません。これらの文字名を持つファイル名はデータベースに書き込まれる可能性がありますが、これらのファイル名を PHP コードの関数に渡すと、ファイルが見つからないなどのエラー メッセージが生成されます。
問題のある文字を含むファイル名を getimagesize() に渡そうとしたときに、これを発見しました。
utf8_encode に必要なのは、126 と 160 の間の包括的な値の変換を除外する一方で、他のすべての文字 (または任意の文字、文字、またはユーザーの欲望の文字範囲の変換を含む) の変換を除外するフィルターです。 、提供された理由により)。
私が考案したソリューションには、次に示す 2 つの関数と、それに続くアプリケーションが必要です。
// With thanks to Mark Baker for this function, posted elsewhere on StackOverflow
function _unichr($o) {
if (function_exists('mb_convert_encoding')) {
return mb_convert_encoding('&#'.intval($o).';', 'UTF-8', 'HTML-ENTITIES');
} else {
return chr(intval($o));
}
}
// For each character where value is inclusively between 126 and 160,
// write out the _unichr of the character, else write out the UTF8_encode of the character
function smart_utf8_encode($source) {
$text_array = str_split($source, 1);
$new_string = '';
foreach ($text_array as $character) {
$value = ord($character);
if ((126 <= $value) && ($value <= 160)) {
$new_string .= _unichr($character);
} else {
$new_string .= utf8_encode($character);
}
}
return $new_string;
}
$file_name = "abcdefghijklmnopqrstuvxyz~€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”–—˜™š›œžŸ¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ.jpg";
// This MUST be done first:
$file_name = iconv('UTF-8', 'WINDOWS-1252', $file_name);
// Next, smart_utf8_encode the variable (from encoding.inc.php):
$file_name = smart_utf8_encode($file_name);
// Now the file name may be passed to getimagesize(), etc.
$getimagesize = getimagesize($file_name);
PHP7 (ナンバリングで 6 はスキップされていますか?) だけが特定の文字値を除外するために utf8_encode() にフィルターを含める場合、これは必要ありません。