PHP:我如何从FTP服务器读取.txt文件到变量中? [英] PHP: How do I read a .txt file from FTP server into a variable?
问题描述
我有两台服务器。我已连接到.txt文件。
我需要获取.txt文件内容并将它们放入$变量中。这是我的代码,
不起作用:
$ ftp_server = $ _POST [site];
$ path = $ _POST [path];
$ ftp_user_name = $ _POST [username];
$ ftp_user_pass = $ _POST [pass];
//设置基本连接
$ conn_id = ftp_connect($ ftp_server);
//使用用户名和密码登录
$ 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从一个文本文件复制到一个变量$内容。我知道一个变量不属于这个参数,但我现在就卖掉了。我不知道如何阅读这个.txt文件。
有没有办法用PHP FTP函数来做到这一点?
谢谢
现在,当我尝试这种方式时:
$ fileContents = file_get_contents('ftp:// username:pa ssword @ hostname / path / to / file');
它提供了一个550错误,说明.txt文件不是常规文件。为什么我得到这个错误?
再次感谢
以下是我得到的官方错误:
解决方案
p>试试这个,这段代码来自 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;
?>
如果有错误,请将文件权限更改为777或775.希望这有助于您。
I have two servers. I already have a .txt file in the one I'm connecting to.
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);
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.
Is there a way to do this with the PHP FTP functions?
Thanks
Now, when I try this:
$fileContents = file_get_contents('ftp://username:password@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?
Thanks again
Here is the official error I'm getting:
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
Try this, this code is from http://www.php.net/manual/en/function.fread.php
<?
$filename = "ftp://username:password@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.
这篇关于PHP:我如何从FTP服务器读取.txt文件到变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!