PHP:如何将 .txt 文件从 FTP 服务器读取到变量中? [英] PHP: How do I read a .txt file from FTP server into a variable?

查看:15
本文介绍了PHP:如何将 .txt 文件从 FTP 服务器读取到变量中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两台服务器.我正在连接的文件中已经有一个 .txt 文件.

I have two servers. I already have a .txt file in the one I'm connecting to.

我需要获取 .txt 文件内容并将它们放入 $ 变量中.这是我的代码不工作:

I need to get the .txt file contents and put them into a $ variable. Here is my code that doesnt work:

$ftp_server = $_POST["site"];
$path = $_POST["path"];
$ftp_user_name = $_POST["username"];
$ftp_user_pass = $_POST["pass"];

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

ftp_get($conn_id, $content,"/" . $_POST["path"] . "/" . "#" . $result["id"]
. " - " .    $result["LastName"] . ", " . $result["FirstName"] . "/" . $_POST["title"] . ".txt", FTP_ASCII);

这段代码连接到 FTP,我使用 ftp_get 从一个文本文件复制到一个名为 $content 的变量.我知道一个变量不属于这个参数,但我现在被卖掉了.我不知道如何阅读这个 .txt 文件.

This code connects to the FTP and I used the ftp_get to copy from one text file to a variable called $content. I know a variable doesnt belong in this parameter, but I'm sold right now. I have no idea how to read this .txt file.

有没有办法用 PHP FTP 函数做到这一点?

Is there a way to do this with the PHP FTP functions?

谢谢

现在,当我尝试这个时:

Now, when I try this:

$fileContents = file_get_contents('ftp://username:pa‌​ssword@hostname/path/to/file');

它给出 550 错误,说明 .txt 文件不是常规文件.为什么会出现此错误?

It gives a 550 error saying that the .txt file is not a regular file. Why am I getting this error?

再次感谢

这是我得到的官方错误:

Here is the official error I'm getting:

警告:fopen(ftp://...@goldenbooklet.com/101Notebook 备份: 22-08-2013/#11 - Cappucci, Ryan/queen.txt) [function.fopen]: 无法打开流: FTP 服务器报告 550/101笔记本备份: 22-08-2013/: 不是/中的常规文件home/content/34/11614434/html/adminpdo.php 在第 269 行

Warning: fopen(ftp://...@goldenbooklet.com/101Notebook Backup: 22-08-2013/#11 - Cappucci, Ryan/queen.txt) [function.fopen]: failed to open stream: FTP server reports 550 /101Notebook Backup: 22-08-2013/: not a regular file in /home/content/34/11614434/html/adminpdo.php on line 269

推荐答案

file_get_contents 是最简单的解决方案:

The file_get_contents is the easiest solution:

$contents = file_get_contents('ftp://username:pa‌​ssword@hostname/path/to/file');

如果它不起作用,可能是因为您没有在 PHP 中启用 URL 包装器.

If it does not work, it's probably because you do not have URL wrappers enabled in PHP.

如果您需要更好地控制读取(传输模式、被动模式、偏移量、读取限制等),请使用 ftp_fget 带有 php://temp(或 php://memory)流:

If you need greater control over the reading (transfer mode, passive mode, offset, reading limit, etc), use the ftp_fget with a handle to the php://temp (or the php://memory) stream:

$conn_id = ftp_connect('hostname');

ftp_login($conn_id, 'username', 'password');

ftp_pasv($conn_id, true);

$h = fopen('php://temp', 'r+');

ftp_fget($conn_id, $h, '/path/to/file', FTP_BINARY, 0);

$fstats = fstat($h);
fseek($h, 0);
$contents = fread($h, $fstats['size']); 

fclose($h);
ftp_close($conn_id);

(添加错误处理)

这篇关于PHP:如何将 .txt 文件从 FTP 服务器读取到变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆