简体   繁体   English

通过ftp下载PHP文件,如何用utf8做到这一点?

[英]PHP file downloading through ftp, how to do this with utf8 in mind?

There is a .TXT file with utf8 encoding (if i'm correct) on a external FTP server. 在外部FTP服务器上有一个带有utf8编码的.TXT文件(如果我是正确的)。 I want to download this through a php script to my own ftp server. 我想通过php脚本下载到我自己的ftp服务器。 So i wrote a script, but when i look at the .txt, i see characters with a character that should be é looks like ën. 所以我写了一个脚本,但是当我看到.txt时,我看到的字符应该是é看起来像ñ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? 顺便说一句,有人知道如何通过按空格四次不缩进每一行来让生活更容易在stackoverflow上显示代码吗?

You're using binary transfer, so it's downloading it from the server as it is. 您正在使用二进制传输,因此它将从服务器上下载它。 If the file is incorrect after the download, so it is on the server. 如果下载后文件不正确,那么它就在服务器上。

The PHP code is probably fine; PHP代码可能很好; it doesn't care what encoding the file is in, it's just copying the raw file data via FTP. 它不关心文件的编码,它只是通过FTP复制原始文件数据。

The more likely problem is that your TXT file reader doesn't handle UTF-8 encoding. 更可能的问题是您的TXT文件阅读器不处理UTF-8编码。 Have you checked your text file reader can display UTF-8 characters in other files? 你检查过你的文本文件阅读器可以在其他文件中显示UTF-8字符吗?

Bynary ftp transfer doesn't touch the character encoding of the text file. Bynary ftp传输不会触及文本文件的字符编码。 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. 如果它们看起来一样,这可能只意味着两件事:1。服务器上的文件不是UTF-8,这意味着副本也不是。 2. Your editor/viewer doesn't support UTF-8 (or it doesn't know that the text should be displayed as UTF-8). 2.您的编辑/查看器不支持UTF-8(或者它不知道文本应该显示为UTF-8)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM