php操作ftp打开中文文件夹错误 2021-04-25 PHP 暂无评论 2401 次阅读 错误提示如下 ``` ftp_chdir(): No mapping for the Unicode character exists in the target multi-byte code page. ``` windows iis ftp 服务器已开启允许UTF8编码。 使用php的ftp_chdir方法进入中文目录出错。 采用 ``` $str = mb_convert_encoding("共享文件夹","gbk" , "auto"); $str = mb_convert_encoding("共享文件夹","gb2312" , "auto"); iconv('UTF-8', 'GBK//IGNORE', "你好") ``` 都可以正确进入中文目录,其中mb_convert_encoding方法需要开启扩展 1、windows 服务器环境 编辑 php.ini 文件,将`; extension=php_mbstring.dll`前面的 ; 去掉,重启服务器。 2、Linux 服务器环境 在编译配置时加入` --enable-mbstring=cn `编译参数 ,再进行PHP的编译安装。 最终方法 ``` $str = iconv('UTF-8', 'GBK', "你好"); ftp_chdir($conn_id, $str); ``` 解释一 PHP传给JS字符串用ecsape转换加到url里,又用PHP接收,再用网上找的unscape函数转换一下,这样得到的字符串是UTF-8的,但我需要的是GB2312,于是用iconv转换开始是这样用的 ``` $str = iconv(‘UTF-8’, ‘GB2312’, unescape(isset($_GET[‘str’])? $_GET[‘str’]:”)); ``` 上线后报一堆这样的错 ``` iconv() : Detected an illegal character in input string ``` 考虑到GB2312字符集比较小,换个大的吧,于是改成GBK: ``` $str = iconv(‘UTF-8’, ‘GBK’, unescape(isset($_GET[‘str’])? $_GET[‘str’]:”)); ``` 上线后还是报同样的错! 再认真读手册,发现有这么一段: If you append the string //TRANSLIT to out_charset transliteration is activated. This means that when a character can’t be represented in the target charset, it can be approximated through one or several similarly looking characters. If you append the string //IGNORE, characters that cannot be represented in the target charset are silently discarded. Otherwise, str is cut from the first illegal character. 于是改成 ``` $str = iconv(‘UTF-8’, ‘GBK//IGNORE’, unescape(isset($_GET[‘str’])? $_GET[‘str’]:”)); ``` 本地测试//IGNORE能忽略掉它不认识的字接着往下转,并且不报错,而//TRANSLIT是截掉它不认识的字及其后面的内容,并且报错。//IGNORE是我需要的。 现在等待上线看结果(这样不是好的做法,继续琢磨手册,上网搜搜看),呵呵。。。 在网上找到下面这篇文章,发现mb_convert_encoding也可以,但效率比iconv差。 转换字符串编码iconv与mb_convert_encoding的区别 ``` iconv — Convert string to requested character encoding(PHP 4 >= 4.0.5, PHP 5) mb_convert_encoding — Convert character encoding(PHP 4 >= 4.0.6, PHP 5) ``` 用法: ``` string mb_convert_encoding ( string str, string to_encoding [, mixed from_encoding] ) ``` 需要先启用 mbstring 扩展库,在 php.ini里将`; extension=php_mbstring.dll `前面的 ; 去掉 ``` string iconv ( string in_charset, string out_charset, string str ) ``` 注意: 第二个参数,除了可以指定要转化到的编码以外,还可以增加两个后缀://TRANSLIT 和 //IGNORE, 其中: //TRANSLIT 会自动将不能直接转化的字符变成一个或多个近似的字符, //IGNORE 会忽略掉不能转化的字符,而默认效果是从第一个非法字符截断。 Returns the converted string or FALSE on failure. 使用: 1. 发现iconv在转换字符”-“到gb2312时会出错,如果没有ignore参数,所有该字符后面的字符串都无法被保存。不管怎么样,这个”-“都无法转换成功,无法输出。另外mb_convert_encoding没有这个bug. 2. mb_convert_encoding 可以指定多种输入编码,它会根据内容自动识别,但是执行效率比iconv差太多;如: ``` $str = mb_convert_encoding($str,”euc-jp”,”ASCII,JIS,EUC-JP,SJIS,UTF- 8″); ``` `“ASCII,JIS,EUC-JP,SJIS,UTF-8”`的顺序不同效果也有差异 3. 一般情况下用 iconv,只有当遇到无法确定原编码是何种编码,或者iconv转化后无法正常显示时才用mb_convert_encoding 函数 from_encoding is specified by character code name before conversion. it can be array or string – comma separated enumerated list. If it is not specified, the internal encoding will be used. ``` $str = mb_convert_encoding($str, “UCS-2LE”, “JIS, eucjp-win, sjis-win”); $str = mb_convert_encoding($str, “EUC-JP’, “auto”); ``` 例子: ``` $content = iconv(“GBK”, “UTF-8”, $content); $content = mb_convert_encoding($content, “UTF-8”, “GBK”); ``` 解释二 The error message comes from the FTP server. The problem is that the FTP server cannot interpret these unicode filenames. Please check if UTF8 feature is enabled for the connection. From an existing answer on stackoverflow: It is not enough for you just to encode your string as UTF8 and send it as filename to FTP server. In the past all FTP servers understood ASCII only and nowadays to maintain backward compatibility - even if they are Unicode aware - when they start they treat all filenemes as ASCII too. To make it all work you (your program) must first check what your server is capable of. Servers send their features after client connects - in your case you must check for FEAT UTF8. If your server sends that - it means it understands UTF8. Nevertheless - even if it understands it - you must tell it explicitly that from now on you will send your filenames UTF8 encoded and now it is the stuff that your program lacks (as your server supports utf8 as you've stated). Your client must send to FTP server the following OPTS UTF8 ON. After sending that you may use UTF8 or speak UTF8-ish (so to speak) to your sever. Read here for details Internationalization of the File Transfer Protocol Sources: https://stackoverflow.com/a/19903611/1305200 https://wiki.filezilla-project.org/Character_Encoding 参考资料 http://www.4u4v.net/php-chu-xian-iconv-detected-an-illegal-character-in-input-string-di-jie-jue-ban-fa-ji-zhuan-huan-zi-fu-chuan-bian-ma-iconv-yu-mb_convert_encoding-di-qu-bie.html https://stackoverflow.com/questions/39269080/ruby-netftp-downloading-files-from-a-server 标签: 中文, ftp 本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。