使用PHP脚本检查FTP状态代码

时间:2021-12-26 07:13:56

I have a script that checks responses from HTTP servers using the PEAR HTTP classes. However, I've recently found that the script fails on FTP servers (and probably anything that's not HTTP or HTTPS). I tried Google, but didn't see any scripts or code that returned the server status code from servers other than HTTP servers.

我有一个脚本,使用PEAR HTTP类检查来自HTTP服务器的响应。但是,我最近发现脚本在FTP服务器上失败了(可能是非HTTP或HTTPS的任何东西)。我试过谷歌,但没有看到任何脚本或代码从HTTP服务器以外的服务器返回服务器状态代码。

How can I find out the status of a newsgroup or FTP server using PHP?

如何使用PHP查找新闻组或FTP服务器的状态?

EDIT: I should clarify that I am interested only in the ability to read from an FTP server and the directory that I specify. I need to know if the server is dead/gone, I'm not authorized to read, etc.

编辑:我应该澄清一下,我只对从FTP服务器和我指定的目录中读取的能力感兴趣。我需要知道服务器是否已经死亡,我无权阅读,等等。

Please note that, although most of the time I'm language agnostic, the entire website is PHP-driven, so a PHP solution would be the best for easy of maintainability and extensibility in the future.

请注意,尽管大部分时间我都是语言不可知的,但整个网站都是由PHP驱动的,因此PHP解决方案将是最好的,以便将来易于维护和扩展。

3 个解决方案

#1


3  

HTTP works slightly differently than FTP though unfortunately. Although both may look the same in your browser, HTTP works off the basis of URI (i.e. to access resource A, you have an identifier which tells you how to access that).

不幸的是,HTTP与FTP的工作方式略有不同。尽管两者在浏览器中看起来都相同,但HTTP基于URI工作(即,为了访问资源A,您有一个标识符,告诉您如何访问它)。

FTP is very old school server driven. Even anonymous FTP is a bit of a hack, since you still supply a username and password, it's just defined as "anonymous" and your email address.

FTP是非常古老的学校服务器驱动。即使是匿名FTP也是一种黑客,因为你仍然提供用户名和密码,它只是被定义为“匿名”和你的电子邮件地址。

Checking if an FTP server is up means checking

检查FTP服务器是否启动意味着检查

  1. That you can connect to the FTP server

    您可以连接到FTP服务器

    if (!($ftpfd = ftp_connect($hostname))) { ... }

    if(!($ ftpfd = ftp_connect($ hostname))){...}

  2. That you can login to the server:

    您可以登录到服务器:

    if (!ftp_login($ftpfd, $username, $password)) { ... }

    if(!ftp_login($ ftpfd,$ username,$ password)){...}

  3. Then, if there are further underlying resources that you need to access to test whether a particular site is up, then use an appropiate operation on them. e.g. on a file, maybe use ftp_mdtm() to get the last modified time or on a directory, see if ftp_nlist() works.

    然后,如果您需要访问其他基础资源以测试特定站点是否已启动,则对它们使用适当的操作。例如在一个文件上,可能使用ftp_mdtm()来获取最后修改的时间或在目录上,看看ftp_nlist()是否有效。

#2


0  

Wouldn't it be simpler to use the built-in PHP FTP* functionality than trying to roll your own? If the URI is coming from a source outside your control, you would need to check the protocal definition (http:// or ftp://, etc) in order to determine which functionality to use, but that is fairly trivial. If there is now protocal specified (there really should be!) then you could try to default to http.

使用内置的PHP FTP *功能比尝试滚动自己更简单吗?如果URI来自控件外部的源,则需要检查协议定义(http://或ftp://等)以确定要使用的功能,但这非常简单。如果现在指定了protocal(确实应该!)那么你可以尝试默认为http。

#3


0  

If you want to read specific responses you will have to open a socket and read/write data manually.

如果要读取特定响应,则必须手动打开套接字并读取/写入数据。

<?php
$sock = fsockopen($hostname, $port);
?>

Then you'll have to fput/fread data back and forth.
This will require you to read up on the FTP protocol.

然后你必须来回传输数据。这将要求您阅读FTP协议。

#1


3  

HTTP works slightly differently than FTP though unfortunately. Although both may look the same in your browser, HTTP works off the basis of URI (i.e. to access resource A, you have an identifier which tells you how to access that).

不幸的是,HTTP与FTP的工作方式略有不同。尽管两者在浏览器中看起来都相同,但HTTP基于URI工作(即,为了访问资源A,您有一个标识符,告诉您如何访问它)。

FTP is very old school server driven. Even anonymous FTP is a bit of a hack, since you still supply a username and password, it's just defined as "anonymous" and your email address.

FTP是非常古老的学校服务器驱动。即使是匿名FTP也是一种黑客,因为你仍然提供用户名和密码,它只是被定义为“匿名”和你的电子邮件地址。

Checking if an FTP server is up means checking

检查FTP服务器是否启动意味着检查

  1. That you can connect to the FTP server

    您可以连接到FTP服务器

    if (!($ftpfd = ftp_connect($hostname))) { ... }

    if(!($ ftpfd = ftp_connect($ hostname))){...}

  2. That you can login to the server:

    您可以登录到服务器:

    if (!ftp_login($ftpfd, $username, $password)) { ... }

    if(!ftp_login($ ftpfd,$ username,$ password)){...}

  3. Then, if there are further underlying resources that you need to access to test whether a particular site is up, then use an appropiate operation on them. e.g. on a file, maybe use ftp_mdtm() to get the last modified time or on a directory, see if ftp_nlist() works.

    然后,如果您需要访问其他基础资源以测试特定站点是否已启动,则对它们使用适当的操作。例如在一个文件上,可能使用ftp_mdtm()来获取最后修改的时间或在目录上,看看ftp_nlist()是否有效。

#2


0  

Wouldn't it be simpler to use the built-in PHP FTP* functionality than trying to roll your own? If the URI is coming from a source outside your control, you would need to check the protocal definition (http:// or ftp://, etc) in order to determine which functionality to use, but that is fairly trivial. If there is now protocal specified (there really should be!) then you could try to default to http.

使用内置的PHP FTP *功能比尝试滚动自己更简单吗?如果URI来自控件外部的源,则需要检查协议定义(http://或ftp://等)以确定要使用的功能,但这非常简单。如果现在指定了protocal(确实应该!)那么你可以尝试默认为http。

#3


0  

If you want to read specific responses you will have to open a socket and read/write data manually.

如果要读取特定响应,则必须手动打开套接字并读取/写入数据。

<?php
$sock = fsockopen($hostname, $port);
?>

Then you'll have to fput/fread data back and forth.
This will require you to read up on the FTP protocol.

然后你必须来回传输数据。这将要求您阅读FTP协议。