CURL FTP 连接仅在托管时被拒绝

我使用 PHP 和 Curl 连接到 FTP 服务器,奇怪的是,当我在本地设置中尝试我的代码时,它以隐式 TLS 和显式 TLS 方式工作,但是当我在任何服务器上运行相同的代码时,它不会不工作并返回Connection refused。


PHP代码是:


<?php


function simple_list_test() {

    $curlopts = [];

    $debug = true;

    $return = null;

    

    $curlopts[CURLOPT_USERPWD] = "{$user}:{$password}";

    $curlopts[CURLOPT_SSL_VERIFYPEER] = false;

    $curlopts[CURLOPT_SSL_VERIFYHOST] = false;

    $curlopts[CURLOPT_FTP_SSL] = CURLFTPSSL_TRY;

    $curlopts[CURLOPT_FTPSSLAUTH] = CURLFTPAUTH_TLS;

    $curlopts[CURLOPT_RETURNTRANSFER] = true;

    $curlopts[CURLOPT_URL] = "ftp://ftp.avidafinance.com:21/"; // I tried with ftps:// protocol too it works on local but not when you run it on a hosting

    $curlopts[CURLOPT_FTPLISTONLY] = 1;

    $curlopts[CURLOPT_UPLOAD] = 0;

    $curlopts[CURLOPT_RETURNTRANSFER] = 1;


    $ch = curl_init();


    foreach($curlopts as $key => $value) {

        curl_setopt($ch, $key, $value);

    }


    if ($debug) {

        curl_setopt($ch, CURLOPT_VERBOSE, true);

        $verbose = fopen('php://temp', 'w+');

        curl_setopt($ch, CURLOPT_STDERR, $verbose);

    }


    $return = curl_exec($ch);


    if ($debug) {

        rewind($verbose);

        $verboseLog = stream_get_contents($verbose);

        echo "Verbose information:\n<pre>", htmlspecialchars($verboseLog), "</pre>\n";

    }


    if ($error = curl_error($ch)) {

        throw new Exception($error);

    }


    return $return;

}

我花了一整天的时间,尝试了很多方法,包括设置CURLOPT_PORT,设置ftps://协议,增加超时,包括服务器.crt和.pem带有curl的证书,但没有一个起作用,所以我想知道是否有人可以帮助我,

简而言之,我需要一个Explicit FTP Over TLS连接,我尝试过curlftp_ssl_connect fsockets, .... 它们都不起作用,我将不胜感激任何帮助,它不一定必须与curl一起使用



Cats萌萌
浏览 89回答 3
3回答

眼眸繁星

经过几周的尝试,我发现正如您在详细信息中看到的那样,连接和登录工作正常,因为它们位于端口 21 上,但当涉及到目录列表和上传/下载时,因为 FTP 服务器使用的是他们的端口不同,它拒绝连接,这是由于我的托管提供商的防火墙不允许这些端口上的传出连接,我要求他们打开端口范围,问题解决了

桃花长相依

让我们总结一下这里的内容:在主动模式 FTP 中,客户端从随机非特权端口 (N > 1023) 连接到 FTP 服务器的命令端口(端口 21)。和在被动模式 FTP 中,客户端发起到服务器的两个连接,解决了防火墙过滤从服务器到客户端的传入数据端口连接的问题。打开 FTP 连接时,客户端会在本地打开两个随机非特权端口(N > 1023 和 N+1)。由于非限制端口策略,这在您的计算机上有效,很高兴知道这在服务器上不起作用,在服务器上您不能只是“开放端口”。我有一个类似的问题,我用 Python ftputil 库解决了这个问题。

HUWWW

您尝试过被动模式吗?$curlopts[CURLOPT_FTP_USE_EPRT]&nbsp;=&nbsp;1;
打开App,查看更多内容
随时随地看视频慕课网APP