【问题标题】:Connection of FTP and read xml file using php使用php连接FTP并读取xml文件
【发布时间】:2018-02-19 18:56:08
【问题描述】:

我已通过 PHP 连接 FTP 并尝试读取 .xml 文件,但我收到警告说找不到文件。

$date =  date('d');

$path = dirname(__FILE__)."/data/".$date;

$ftp_server = "xyz.com";

$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");

$login = ftp_login($ftp_conn, 'uname', 'pwd');

$file_list = ftp_nlist($ftp_conn, ".");

$file_lists = ftp_nlist($ftp_conn,$file_list[2].'/'.date("Y/m/d"));

ftp_pasv($login, true);
ob_start();
ftp_get($ftp_conn,$path,$file_list[2].'/'.date("Y/m/d").'/'.$file_lists[0], FTP_ASCII);
$data = ob_get_contents();
ob_end_clean();
ftp_close($ftp_conn);        
var_dump($data);

如何读取.xml 文件?

【问题讨论】:

  • 尝试var_dump($file_lists)
  • 如果我使用它而不是获取文件列表,但我想读取每个文件并获取所有数据
  • 1.您在这里不需要输出缓冲 2. 您在$file_lists 数组中看到您的文件吗?
  • 是的,我可以看到文件列表,所以我使用了$file_lists[0]

标签: php xml wordpress ftp


【解决方案1】:

事实上ftp_get() 只是下载它而不是阅读内容。更改读取远程文件以输出的路径:

ob_start();
$result = ftp_get($ftp_conn, "php://output", $file_list[2].'/'.date("Y/m/d").'/'.$file_lists[0], FTP_ASCII);
$data = ob_get_contents();
ob_end_clean();

或读取本地文件:

ftp_get($ftp_conn, $path, $file_list[2].'/'.date("Y/m/d").'/'.$file_lists[0], FTP_ASCII);
$data = file_get_contents($path);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-21
    • 1970-01-01
    • 1970-01-01
    • 2015-07-26
    • 1970-01-01
    相关资源
    最近更新 更多