久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

上傳文件名中的UTF-8字符在文件上傳時混亂

UTF-8 characters in uploaded file name are jumbled on file upload(上傳文件名中的UTF-8字符在文件上傳時混亂)
本文介紹了上傳文件名中的UTF-8字符在文件上傳時混亂的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我在 IIS7 上運行一個系統.頁面 META 標簽的編碼為 UTF-8,根據 Chrome 菜單,實際編碼看起來是相同的.

I'm running a system on IIS7. The page META tag has the encoding as UTF-8, and the real encoding would appear to be the same according to the Chrome menu.

當我上傳名稱中帶有長連字符"(–")的文件時,它會被轉換為垃圾字符(–").

When I upload a file with a "long hyphen" in its name ("–") it gets converted to junk characters ("a€"").

垃圾字符保存在MySQL中,服務器上文件的文件名也有垃圾字符.但是,當我從數據庫中提取文件名并用 PHP 顯示時,它會顯示正確的連字符.

The junk characters are saved in MySQL and the file name of the file on the server also has the junk characters. However when I pull the file name from the database and display it with PHP, it displays with the correct hyphen.

有沒有辦法將文件名存儲為 UTF-8?當我嘗試此代碼時,出現錯誤:

Is there any way to have the file name stored as UTF-8? When I try this code I get an error:

$fn = iconv("CP-1252", "UTF-8", $file['name']);
debug($fn);

Notice (8): iconv(): Wrong charset, conversion from `CP-1252' to `UTF-8' is not allowed

--

幾個月后更新!所以這個問題與 Windows 上的 PHP 錯誤有關:http://bugs.php.net/bug.php?id=47096

Update several months later! So this problem is related to a PHP bug on Windows: http://bugs.php.net/bug.php?id=47096

Unicode 字符在 move_upload_file 上被 PHP 破壞 - 盡管我也看到了重命名和 ZipArchive 的問題,所以我認為這是 PHP 和 Windows 的普遍問題.

Unicode characters get mangled by PHP on move_upload_file - although I have also seen the issue with rename and ZipArchive so I think it's a general issue with PHP and Windows.

我從 Wordpress 找到了一個解決方法 此處.我必須使用損壞的文件名存儲文件,然后在下載/電子郵件/顯示時對其進行清理.

I have adapted a workaround from Wordpress found here. I have to store the file with the mangled file name and then sanitize it on download/email/display.

以下是我正在使用的改編方法,以防將來對某人有用.如果您在下載/通過電子郵件發送之前嘗試壓縮文件,或者您需要將文件寫入網絡共享,這仍然沒有多大用處.

Here are the adapted methods I'm using in case it's of use to someone in future. This still isn't much use if you're trying to zip files before downloading/emailing or you need to write the files to a network share.

public static function sanitizeFilename($filename, $utf8 = true)
{
if ( self::seems_utf8($filename) == $utf8 )
    return $filename;

// On Windows platforms, PHP will mangle non-ASCII characters, see http://bugs.php.net/bug.php?id=47096
if ( 'WIN' == substr( PHP_OS, 0, 3 ) ) {
        if(setlocale( LC_CTYPE, 0 )=='C'){ // Locale has not been set and the default is being used, according to answer by Colin Morelli at http://stackoverflow.com/questions/13788415/how-to-retrieve-the-current-windows-codepage-in-php
                // thus, we force the locale to be explicitly set to the default system locale
                $codepage = 'Windows-' . trim( strstr( setlocale( LC_CTYPE, '' ), '.' ), '.' );
        }
        else {
                $codepage = 'Windows-' . trim( strstr( setlocale( LC_CTYPE, 0 ), '.' ), '.' );
        }
        $charset = 'UTF-8';
        if ( function_exists( 'iconv' ) ) {

                if ( false == $utf8 ){
                    $filename = iconv( $charset, $codepage . '//IGNORE', $filename );
                }
                else {
                    $filename = iconv( $codepage, $charset, $filename );
                }
        } elseif ( function_exists( 'mb_convert_encoding' ) ) {
                if ( false == $utf8 )
                        $filename = mb_convert_encoding( $filename, $codepage, $charset );
                else
                        $filename = mb_convert_encoding( $filename, $charset, $codepage );
        }
}

return $filename;       

}

public static function seems_utf8($str) {
    $length = strlen($str);
    for ($i=0; $i < $length; $i++) {
            $c = ord($str[$i]);
            if ($c < 0x80) $n = 0; # 0bbbbbbb
            elseif (($c & 0xE0) == 0xC0) $n=1; # 110bbbbb
            elseif (($c & 0xF0) == 0xE0) $n=2; # 1110bbbb
            elseif (($c & 0xF8) == 0xF0) $n=3; # 11110bbb
            elseif (($c & 0xFC) == 0xF8) $n=4; # 111110bb
            elseif (($c & 0xFE) == 0xFC) $n=5; # 1111110b
            else return false; # Does not match any model
            for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ?
                    if ((++$i == $length) || ((ord($str[$i]) & 0xC0) != 0x80))
                            return false;
            }
    }
    return true;

}

推薦答案

UPDATE實際上,這是 Windows 上的 PHP 錯誤.有如下解決方法,但我見過的最佳解決方案是使用 WFIO 擴展.這個擴展為文件流提供了一個新的協議 wfio:// 并允許 PHP 正確處理 Windows 文件系統上的 UTF-8 字符.wfio://支持多??種PHP函數,包括fopen、scandir、mkdir、copy、rename等

UPDATE Indeed this is a PHP bug on Windows. There are workarounds like below, but the best solution I have seen is to use the WFIO extension. This extension provides a new protocol wfio:// for file streams and allows PHP to properly handle UTF-8 characters on the Windows file-system. wfio:// supports a number of PHP functions including fopen, scandir, mkdir, copy, rename, etc.

原始解決方案

所以這個問題與 Windows 上的 PHP 錯誤有關:http://bugs.php.net/bug.php?id=47096

So this problem is related to a PHP bug on Windows: http://bugs.php.net/bug.php?id=47096

Unicode 字符在 move_upload_file 上被 PHP 破壞 - 盡管我也看到了重命名和 ZipArchive 的問題,所以我認為這是 PHP 和 Windows 的普遍問題.

Unicode characters get mangled by PHP on move_upload_file - although I have also seen the issue with rename and ZipArchive so I think it's a general issue with PHP and Windows.

我從 Wordpress 找到了一個解決方法 此處.我必須使用損壞的文件名存儲文件,然后在下載/電子郵件/顯示時對其進行清理.

I have adapted a workaround from Wordpress found here. I have to store the file with the mangled file name and then sanitize it on download/email/display.

以下是我正在使用的改編方法,以防將來對某人有用.如果您在下載/通過電子郵件發送之前嘗試壓縮文件,或者您需要將文件寫入網絡共享,這仍然沒有多大用處.

Here are the adapted methods I'm using in case it's of use to someone in future. This still isn't much use if you're trying to zip files before downloading/emailing or you need to write the files to a network share.

public static function sanitizeFilename($filename, $utf8 = true)
{
if ( self::seems_utf8($filename) == $utf8 )
    return $filename;

// On Windows platforms, PHP will mangle non-ASCII characters, see http://bugs.php.net/bug.php?id=47096
if ( 'WIN' == substr( PHP_OS, 0, 3 ) ) {
        if(setlocale( LC_CTYPE, 0 )=='C'){ // Locale has not been set and the default is being used, according to answer by Colin Morelli at http://stackoverflow.com/questions/13788415/how-to-retrieve-the-current-windows-codepage-in-php
                // thus, we force the locale to be explicitly set to the default system locale
                $codepage = 'Windows-' . trim( strstr( setlocale( LC_CTYPE, '' ), '.' ), '.' );
        }
        else {
                $codepage = 'Windows-' . trim( strstr( setlocale( LC_CTYPE, 0 ), '.' ), '.' );
        }
        $charset = 'UTF-8';
        if ( function_exists( 'iconv' ) ) {

                if ( false == $utf8 ){
                    $filename = iconv( $charset, $codepage . '//IGNORE', $filename );
                }
                else {
                    $filename = iconv( $codepage, $charset, $filename );
                }
        } elseif ( function_exists( 'mb_convert_encoding' ) ) {
                if ( false == $utf8 )
                        $filename = mb_convert_encoding( $filename, $codepage, $charset );
                else
                        $filename = mb_convert_encoding( $filename, $charset, $codepage );
        }
}

return $filename;       

}

public static function seems_utf8($str) {
    $length = strlen($str);
    for ($i=0; $i < $length; $i++) {
            $c = ord($str[$i]);
            if ($c < 0x80) $n = 0; # 0bbbbbbb
            elseif (($c & 0xE0) == 0xC0) $n=1; # 110bbbbb
            elseif (($c & 0xF0) == 0xE0) $n=2; # 1110bbbb
            elseif (($c & 0xF8) == 0xF0) $n=3; # 11110bbb
            elseif (($c & 0xFC) == 0xF8) $n=4; # 111110bb
            elseif (($c & 0xFE) == 0xFC) $n=5; # 1111110b
            else return false; # Does not match any model
            for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ?
                    if ((++$i == $length) || ((ord($str[$i]) & 0xC0) != 0x80))
                            return false;
            }
    }
    return true;

}

這篇關于上傳文件名中的UTF-8字符在文件上傳時混亂的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Cannot use #39;Object as class name as it is reserved Cake 2.2.x(不能使用 Object 作為類名,因為它是保留的 Cake 2.2.x)
Session is lost after an OAuth redirect(OAuth 重定向后會話丟失)
Pagination Sort in Cakephp 3.x(Cakephp 3.x 中的分頁排序)
CakePHP Shared core for multiple apps(CakePHP 多個應用程序的共享核心)
Login [ Auth-gt;identify() ] always false on CakePHP 3(在 CakePHP 3 上登錄 [ Auth-identify() ] 始終為 false)
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 87 bytes)(致命錯誤:允許的內存大小為 134217728 字節已用盡(嘗試分配 87 字節))
主站蜘蛛池模板: 亚洲高清在线 | 国产精品久久国产精品 | 久草久草久草 | 亚洲欧美精| 欧美久| 国产中文字幕在线 | 国内精品视频在线观看 | 91在线视频免费观看 | 夜夜骑首页 | 亚洲免费精品一区 | 国产精品海角社区在线观看 | 日韩视频1 | 国产视频精品区 | h片在线免费观看 | 日韩国产精品一区二区三区 | 欧美一级二级视频 | 一区二区av | 亚洲www啪成人一区二区麻豆 | 久久不射网 | 欧美日韩高清 | 欧美成视频在线观看 | 欧美专区日韩 | 91大神在线资源观看无广告 | 日韩网站在线观看 | 成人精品福利 | 日韩精品久久一区 | 黄色在线免费观看视频 | h片免费在线观看 | 97超碰人人 | 久久精品国产99国产精品亚洲 | 91精品国产一区二区三区动漫 | 欧美黄色性生活视频 | 色婷婷婷婷色 | www.亚洲精品 | 日韩淫片免费看 | 草在线 | 午夜资源 | 麻豆精品久久 | 亚洲永久在线 | 91久色 | 日操操夜操操 |