There is a .TXT file with utf8 encoding (if i’m correct) on a external FTP server. I want to download this through a php script to my own ftp server.
So i wrote a script, but when i look at the .txt, i see characters with a character that should be é looks like ën.
How can i do this correct? (also, if i run the same script again i want the old file being replaced with a fresh new file). This is my code:
<?php
// connecting with ftp server
$connection_id = ftp_connect('ftp.example.com');
// login with username and password
$login = ftp_login($connection_id, 'username', 'password');
// check connection
if ((!$connection_id) || (!$login)) {
echo 'FTP connection has failed.';
exit();
} else {
echo 'Connection succeeded.';
}
$local_file = 'home/file.TXT';
$server_file = '/file.TXT';
// open file
$handle = fopen($local_file, 'w+');
// try to download txt file and save it locally
if(ftp_fget($connection_id, $handle, $server_file, FTP_BINARY, 0)) {
echo 'Succesfully written to '.$local_file;
} else {
echo 'Not succesfully downloaded!';
}
// close file handler
fclose($handle);
//close the connection
ftp_close($connection_id);
?>
Btw, does anbody know how to make life easier for showing code on stackoverflow by not indenting every single line by pressing on space for four times?
Bynary ftp transfer doesn’t touch the character encoding of the text file. It copies at the bit level. It doesn’t care what kind of data it transfers.
Have you tried to open both local and server file in your text viewer/editor?
If they look the same this could mean only two things:
1. The file is not UTF-8 on the server and this means that the copy will not be either.
2. Your editor/viewer doesn’t support UTF-8 (or it doesn’t know that the text should be displayed as UTF-8).