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

发布于 2024-11-26 14:32:34 字数 1065 浏览 0 评论 0原文

外部 FTP 服务器上有一个 utf8 编码的 .TXT 文件(如果我是正确的)。我想通过 php 脚本将其下载到我自己的 ftp 服务器。 所以我写了一个脚本,但是当我查看 .txt 时,我看到一些字符应该是 é ,看起来像 àn 。 我怎样才能正确地做到这一点? (另外,如果我再次运行相同的脚本,我希望旧文件被替换为新文件)。这是我的代码:

<?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);
?>

顺便说一句,有人知道如何通过按四次空格而不缩进每一行来让在 stackoverflow 上显示代码变得更轻松吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

永不分离 2024-12-03 14:32:34

您使用的是二进制传输,因此它会按原样从服务器下载它。如果下载后文件不正确,那么是在服务器上。

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.

何以笙箫默 2024-12-03 14:32:34

PHP 代码可能没问题;它不关心文件采用什么编码,它只是通过 FTP 复制原始文件数据。

更可能的问题是您的 TXT 文件阅读器不处理 UTF-8 编码。您是否检查过您的文本文件阅读器可以在其他文件中显示 UTF-8 字符?

The PHP code is probably fine; it doesn't care what encoding the file is in, it's just copying the raw file data via FTP.

The more likely problem is that your TXT file reader doesn't handle UTF-8 encoding. Have you checked your text file reader can display UTF-8 characters in other files?

叫嚣ゝ 2024-12-03 14:32:34

二进制 ftp 传输不涉及文本文件的字符编码。它以位级别进行复制。它不关心它传输什么类型的数据。

您是否尝试过在文本查看器/编辑器中打开本地文件和服务器文件?
如果它们看起来相同,这可能只意味着两件事:
1. 文件在服务器上不是 UTF-8,这意味着副本也不是。
2.您的编辑器/查看器不支持UTF-8(或者它不知道文本应该显示为UTF-8)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文