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

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

问题描述

我需要从劳工统计局的公共 FTP 服务器下载数据以进行分析.我正在尝试使用 PHP 来检索列表,但我不确定如何使用公共 FTP 服务器来执行此操作 - 不使用 ftp_login 会导致返回false",并且尝试以匿名身份登录会挂起脚本.

我的代码:

解决方案

你的脚本适合我(查看输出),我得到了一个不错的目录列表.请联系您的 PHP 脚本正在运行的服务器的系统管理员并寻求支持.对我来说,这似乎是一个网络配置问题.

另外,在继续之前始终检查函数返回值是否有错误:

//连接$ftp = ftp_connect("ftp.bls.gov");if (!$ftp) die('无法连接.');//登录$r = ftp_login($ftp, "匿名", "");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 登录,列出一组有限目录的内容,并检索文件.一些网站限制目录的内容列出匿名用户也可以看到.请注意,匿名"用户不是通常允许将文件传输到存档站点,但只能从此类站点检索文件.

传统上,这个特殊的匿名用户帐户接受任何字符串作为密码,尽管通常使用密码客人"或某人的电子邮件(e-mail)地址.一些档案网站现在明确要求用户的电子邮件地址,并且不会允许使用访客"密码登录.提供电子邮件地址是一种礼貌,允许存档站点操作员了解谁在使用他们的服务.

摘自:如何使用匿名 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天全站免登陆