使用PHP访问FTP目录列表 [英] Accessing an FTP directory listing with PHP

查看:303
本文介绍了使用PHP访问FTP目录列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从劳工统计局的公共FTP服务器下载数据进行分析。我尝试使用PHP来检索列表,但我不知道如何使用公共FTP服务器执行此操作 - 使用不返回false的ftp_login结果,并尝试以匿名身份登录时挂起脚本。 / p>

我的代码:

 <?php 
/ /设置基本连接
$ ftp = ftp_connect(ftp.bls.gov);
ftp_login($ ftp,anonymous,);
ftp_pasv($ ftp,true);
var_dump(ftp_rawlist($ ftp,/pub/time.series/la/));
?>


解决方案

您的脚本适合我(查看输出),我得到一个不错的目录列表。请联系您的PHP脚本运行的服务器的系统管理并寻求支持。它看起来像这是一个网络配置问题给我。



另外总是检查函数返回值的错误,继续之前:

  //连接
$ ftp = ftp_connect(ftp.bls.gov);
if(!$ ftp)die('could not connect。');

//登录
$ r = ftp_login($ ftp,anonymous,);
if(!$ r)die('无法登录');

//进入被动模式
$ r = ftp_pasv($ ftp,true);
if(!$ r)die('无法启用被动模式。');

//获取列表
$ r = ftp_rawlist($ ftp,/pub/time.series/la/);
var_dump($ r);







什么是匿名FTP?



匿名FTP是归档站点允许一般访问
存档信息的一种方式。这些网站创建一个名为匿名的特殊
账户。用户匿名限制了存档主机的访问权限
,以及一些操作限制。
实际上,允许的唯一操作是使用FTP登录,
列出了一组有限的目录的内容,并检索
文件。有些网站限制列出
匿名用户可以看到的目录内容。请注意,匿名用户通常不允许将文件传输到存档站点,但只能从这样的站点获取文件。

传统上,这个特殊的匿名用户帐户接受任何字符串
作为密码,不过通常使用密码
guest或其电子邮件(电子邮件)地址。一些存档
网站现在明确要求用户的电子邮件地址,而
不允许使用guest密码登录。提供电子邮件地址
是一种礼节,它允许存档网站运营商了解使用其服务的


摘自:如何使用匿名FTP(RFC 1635)


I need to download data from the Bureau of Labor Statistics' public FTP server for analysis. I'm attempting to use PHP to retrieve a listing, but I'm not sure how to do it with a public FTP server - using no ftp_login results in "false" being returned, and attempting to login as anonymous hangs the script.

My code:

<?php
// set up basic connection
$ftp = ftp_connect("ftp.bls.gov");
       ftp_login($ftp, "anonymous", "");
             ftp_pasv($ftp, true);
var_dump(ftp_rawlist($ftp, "/pub/time.series/la/"));
?>

解决方案

Your script works for me (see output), I get a nice directory listing. Please contact the system administration of the server your PHP script is running and ask for support. It looks like that this is a network configuration issue to me.

Additionally always check function return values for errors before you continue:

// connect
$ftp = ftp_connect("ftp.bls.gov");
if (!$ftp) die('could not connect.');

// login
$r = ftp_login($ftp, "anonymous", "");
if (!$r) die('could not login.');

// enter passive mode
$r = ftp_pasv($ftp, true);
if (!$r) die('could not enable passive mode.');

// get listing
$r = ftp_rawlist($ftp, "/pub/time.series/la/");
var_dump($r);


What is Anonymous FTP?

Anonymous FTP is a means by which archive sites allow general access to their archives of information. These sites create a special account called "anonymous". User "anonymous" has limited access rights to the archive host, as well as some operating restrictions. In fact, the only operations allowed are logging in using FTP, listing the contents of a limited set of directories, and retrieving files. Some sites limit the contents of a directory listing an anonymous user can see as well. Note that "anonymous" users are not usually allowed to transfer files TO the archive site, but can only retrieve files from such a site.

Traditionally, this special anonymous user account accepts any string as a password, although it is common to use either the password "guest" or one's electronic mail (e-mail) address. Some archive sites now explicitly ask for the user's e-mail address and will not allow login with the "guest" password. Providing an e-mail address is a courtesy that allows archive site operators to get some idea of who is using their services.

Excerpt from: How to Use Anonymous FTP (RFC 1635)

这篇关于使用PHP访问FTP目录列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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