8

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.

J. Rahmati
  • 735
  • 10
  • 37
artnikpro
  • 5,487
  • 4
  • 38
  • 40
  • 1
    What happens if you add header('Content-Type: text/html; charset=windows-1251'); to your script? – herrjeh42 Jun 03 '13 at 06:00
  • @jamie0726, good suggesting! It also returned result with correct encoding. It would be great solution in case if I will need to transfer files with a specific encoding. But for me is neccessary to automatically detect the encoding of each file. – artnikpro Jun 06 '13 at 18:38
  • i have this problem , how to fix this? – user3770797 Jul 19 '17 at 06:49

4 Answers4

9

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>"; 
} 
?>
Hugo Delsing
  • 13,803
  • 5
  • 45
  • 72
  • Thanks Hugo, this `mb_convert_encoding($buff[0], "UTF-8", "windows-1251");` works for me but only in case if the filename is in windows-1251 encoding. But it could also be any other encoding, so is there some reliable way to detect it automatically? I tryed to use `mb_detect_encoding` but it doesn't returns the correct encoding :( – artnikpro Jun 03 '13 at 19:23
  • check http://stackoverflow.com/questions/910793/detect-encoding-and-make-everything-utf-8 for an extensive answer on changing encoding to utf-8 – Hugo Delsing Jun 03 '13 at 19:49
  • @artnikpro Why do you think it could be any other encoding? Doesn't the same machine only have a single encoding? Cause if it is not, then you just get bytes (as opposed to chars) and there's no sure way of saying what it is, it might even be some dog encoding for all you know. And even if it does make sense to parse every char as some cyrillic char in some encoding, you still don' know for sure (unless it's a real word in some language, but who has all those dictionaries). So what I'm saying is that this is the best answer you will get as what you (probably) are thinking about is impossible. – inkredibl Jun 04 '13 at 12:56
  • The reference to the other question should be in your main answer. It's brilliant. – Nicholas Shanks Jun 08 '13 at 07:09
3

Many (but not all) ftp servers supports UTF-8 pathnames encoding. You can turn this feature on by issuing 'OPTS UTF8 ON' command before ftp_nlist call.

ftp_raw('OPTS UTF8 ON');
1

First you add content type on your page.

header('Content-Type: text/html; charset=utf-8');

And then try this, hope it helps

str_replace(array('%82','%94','+'),array('&#233;','&#246;',' '),urlencode($folder_name));

It's not the best way, but it works for me, if you url encode a string it changes the awkward characters into e.g. %82... You can then replace these with the HTML codes.

Farhan
  • 13
  • 5
0

you can try using iconv function. Hoping it will solve your problem.

Abani Meher
  • 564
  • 3
  • 17