PHP - FTP文件编码问题 [英] PHP - FTP filename encoding issue

查看:348
本文介绍了PHP - FTP文件编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会简短的。我的FTP函数返回文件名的错误编码

  $ conn_id = ftp_connect(site.com); 
ftp_login($ conn_id,login,pass);
ftp_pasv($ conn_id,true);
$ buff = ftp_nlist($ conn_id,./);
print_r($ buff);

- > // result
array(){
[0] => .txt
}

文件名有Windows-1251编码。 / p>

我尝试通过nodejs连接到FTP,但它也返回一些令人毛骨悚然的东西 - òð.txt



我的桌面客户端(WinSCP)可以正常工作。



PS:使用utf8_encode - 但这也不适用于我。

解决方案

如果编码是你可以尝试使用 mb_convert_encoding 。下面的代码应该输出正确的值。

 <?php 
echo mb_convert_encoding($ buff [0] UTF-8);
//或
echo mb_convert_encoding($ buff [0],UTF-8,windows-1251);
?>

如果它不起作用,可以尝试使用类似于

 <?php 
foreach(mb_list_encodings()as $ chr){
echo mb_convert_encoding($ buff [0] 'UTF-8',$ chr)。:。$ chr。< br>;
}
?>


I will be brief. My FTP function returns wrong encoding of filenames

$conn_id = ftp_connect("site.com");
ftp_login($conn_id, "login", "pass");
ftp_pasv($conn_id, true);
$buff = ftp_nlist($conn_id, "./");
print_r($buff);

->  // result
    array() {
        [0]=> "��.txt"
    }

The file name has Windows-1251 encoding.

I tried to connect to FTP via nodejs but it also returns something creepy — òð.txt.

My desktop client (WinSCP) however works fine with this.

PS: I tried to use utf8_encode - but that's also not working for me.

解决方案

If the encoding is of you could try to change it using mb_convert_encoding. The code below should output the correct value.

<?php
echo mb_convert_encoding($buff[0], "UTF-8");
//or
echo mb_convert_encoding($buff[0], "UTF-8", "windows-1251");
?>

If it doesnt work, you can try to find the right encoding using something like

<?php
foreach(mb_list_encodings() as $chr){
  echo mb_convert_encoding($buff[0], 'UTF-8', $chr)." : ".$chr."<br>"; 
} 
?>

这篇关于PHP - FTP文件编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆