用 PHP 测试 FTP 连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3729694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:54:53  来源:igfitidea点击:

Test FTP connection with PHP

phpftp

提问by Batfan

I am using the PHP script below to test FTP connections. Currently it is printing an array of the files, if it successfully connects.

我正在使用下面的 PHP 脚本来测试 FTP 连接。如果成功连接,当前它正在打印文件数组。

How can I get it to also display a message, if it is able to connect? Like 'Connection Successful'.

如果能够连接,我怎样才能让它也显示一条消息?比如“连接成功”。

$con = ftp_connect($server) or die("Couldn't connect"); 
ftp_login($con,  $username,  $password);
print_r(ftp_nlist($con, "."));
ftp_close($con);

EDIT

编辑

I have it working now but, I've tested this on a few domains I have on a MediaTemple server and they all seem be timing out. Yet, it works with all other domains I have tried. Are their servers blocking the request?

我现在可以工作了,但是,我已经在 MediaTemple 服务器上的几个域上进行了测试,它们似乎都超时了。然而,它适用于我尝试过的所有其他域。他们的服务器是否阻止了请求?

采纳答案by Jakub

Simply do a check if ftp_nlist()is an array.

只需检查是否ftp_nlist()为数组。

Like:

喜欢:

echo is_array(ftp_nlist($con, ".")) ? 'Connected!' : 'not Connected! :(';

echo is_array(ftp_nlist($con, ".")) ? 'Connected!' : 'not Connected! :(';

References:

参考:

回答by mr. w

Both ftp_connect() and ftp_login() return a boolean indicating success. Thus, something like this should do what you want, if I'm interpreting properly:

ftp_connect() 和 ftp_login() 都返回一个表示成功的布尔值。因此,如果我正确解释,这样的事情应该做你想做的:

try {
    $con = ftp_connect($server);
    if (false === $con) {
        throw new Exception('Unable to connect');
    }

    $loggedIn = ftp_login($con,  $username,  $password);
    if (true === $loggedIn) {
        echo 'Success!';
    } else {
        throw new Exception('Unable to log in');
    }

    print_r(ftp_nlist($con, "."));
    ftp_close($con);
} catch (Exception $e) {
    echo "Failure: " . $e->getMessage();
}

回答by simshaun

Hey. I'm new here so maybe posting this late answer is not welcome, but it may help people in the future.

嘿。我是新来的,所以也许不欢迎发布这个迟到的答案,但它可能会在未来帮助人们。

The reason why it's not working with MediaTemple is because they only accept passive connections.

它不与 MediaTemple 一起使用的原因是因为它们只接受被动连接。

After logging in with ftp_login(), simply call ftp_pasv($ftp, TRUE);and you'll be set.

使用 登录后ftp_login(),只需拨打电话ftp_pasv($ftp, TRUE);即可完成设置。

回答by Amar Jeet

Hello I have tried this..Working properly.

你好,我试过这个..工作正常。

set_time_limit(300);//for setting 
$path='/'.date('dmY').'';
$ftp_server='';
$ftp_server_port="";
$ftp_user_name='';
$ftp_user_pass="";

// set up a connection to ftp server
$conn_id = ftp_connect($ftp_server, $ftp_server_port); 
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// check connection and login result
if ((!$conn_id) || (!$login_result)) { 
    echo "Fail</br>";
} else {
    echo "Success</br>";
    // enabling passive mode
    ftp_pasv( $conn_id, true );
    // get contents of the current directory
    $contents = ftp_nlist($conn_id, $path);
    // output $contents
    var_dump($contents);
}

// close the FTP connection
ftp_close($conn_id);

回答by ajmedway

While I agree with the logic in the accepted answer from @Jakub of calling ftp_nlist()and testing data type with is_array(), this can be very slow and cumbersome with particularly large, bloated ftp directories like the ones I am currently working on. And I don't like the idea of creating a blank directory just for testing, which can be renamed/removed later as considered to be unneeded, perhaps by another developer or because you forgot what it was placed there for.

虽然我同意@Jakub 接受的使用 调用ftp_nlist()和测试数据类型的答案中的逻辑,但对于is_array()像我目前正在处理的那些特别大、臃肿的 ftp 目录,这可能会非常缓慢和繁琐。而且我不喜欢创建一个仅用于测试的空白目录的想法,以后可以重命名/删除它,因为它被认为是不需要的,也许是由另一个开发人员或因为您忘记了它放在那里的目的。

I am using a passive ftp connection, so for my purpose on cron scripts that can take a long time to execute and potentially require reconnection, I detect using this:

我使用的是被动 ftp 连接,因此出于我的目的,可能需要很长时间才能执行并可能需要重新连接的 cron 脚本,我使用以下方法进行检测:

function check_connection_status($conn_id) {
    return ftp_pasv($conn_id, true);
}

Issuing a fresh call to ftp_pasv()will make no alteration to the state of the ftp connection, but it will respond true if the connection is active and logged in/false if not so you can program to reconnect again :)

发出新的调用ftp_pasv()不会改变 ftp 连接的状态,但如果连接处于活动状态,它将响应真,如果没有登录/假,则您可以编程以再次重新连接:)

回答by Dolph

Note that you're already dieing when you fail to connect, so you can assume that you are connected. However, you can also check the status of the connection using:

请注意,die当您无法连接时,您已经在ing,因此您可以假设您已连接。但是,您也可以使用以下方法检查连接状态:

echo $con !== FALSE ? 'Connected' : "Couldn't connect";

ftp_connect: Returns a FTP stream on success or FALSE on error.

ftp_connect:成功时返回 FTP 流,错误时返回 FALSE。