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.

3
  • 1
    What happens if you add header('Content-Type: text/html; charset=windows-1251'); to your script? Commented Jun 3, 2013 at 6: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. Commented Jun 6, 2013 at 18:38
  • i have this problem , how to fix this? Commented Jul 19, 2017 at 6:49

4 Answers 4

9
+25

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>"; 
} 
?>
Sign up to request clarification or add additional context in comments.

4 Comments

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 :(
check stackoverflow.com/questions/910793/… for an extensive answer on changing encoding to utf-8
@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.
The reference to the other question should be in your main answer. It's brilliant.
4

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 Comment

It works! Very good ~~~ ftp_raw($conn_id, '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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.