PHP:如何将 .txt 文件从 FTP 服务器读取到变量中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18392321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP: How do I read a .txt file from FTP server into a variable?
提问by Ryan Jeffrey Cappucci
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:
我需要获取 .txt 文件内容并将它们放入 $ 变量中。这是我的代码不起作用:
$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);
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.
此代码连接到 FTP,我使用 ftp_get 从一个文本文件复制到一个名为 $content 的变量。我知道一个变量不属于这个参数,但我现在卖掉了。我不知道如何阅读这个 .txt 文件。
Is there a way to do this with the PHP FTP functions?
有没有办法用 PHP FTP 函数做到这一点?
Thanks
谢谢
Now, when I try this:
现在,当我尝试这个时:
$fileContents = file_get_contents('ftp://username:pa??ssword@hostname/path/to/file');
It gives a 550 error saying that the .txt file is not a regular file. Why am I getting this error?
它给出了 550 错误,说明 .txt 文件不是常规文件。为什么我收到这个错误?
Thanks again
再次感谢
Here is the official error I'm getting:
这是我收到的官方错误:
Warning: fopen(ftp://[email protected]/101NotebookBackup: 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
警告:fopen( ftp://[email protected]/101NotebookBackup: 22-08-2013/#11 - Cappucci, Ryan/queen.txt) [function.fopen]:无法打开流:FTP 服务器报告550 /101Notebook Backup: 22-08-2013/: 不是第 269 行 /home/content/34/11614434/html/adminpdo.php 中的常规文件
回答by Martin Prikryl
The file_get_contents
is the easiest solution:
这file_get_contents
是最简单的解决方案:
$contents = file_get_contents('ftp://username:pa??ssword@hostname/path/to/file');
If it does not work, it's probably because you do not have URL wrappers enabled in PHP.
如果它不起作用,可能是因为您没有在 PHP 中启用 URL 包装器。
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:
如果您需要更好地控制读取(传输模式、被动模式、偏移量、读取限制等),请使用ftp_fget
带有php://temp
(或php://memory
)流的句柄的:
$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);
(add error handling)
(添加错误处理)
回答by Palm Underscorez
Try this, this code is from http://www.php.net/manual/en/function.fread.php
试试这个,这个代码来自http://www.php.net/manual/en/function.fread.php
<?
$filename = "ftp://username:pa??ssword@hostname/path/to/file";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
echo $contents;
?>
If error, please change the file permission to 777 or 775. Hope this may help.
如果出错,请将文件权限更改为 777 或 775。希望这可能会有所帮助。
回答by tobidude
If you've already opened a connection via ftp_connect, this would probably be the best answer:
如果您已经通过 ftp_connect 打开了一个连接,这可能是最好的答案:
ob_start();
$result = ftp_get($conn_id, "php://output", $file, FTP_BINARY);
$data = ob_get_contents();
ob_end_clean();