ftp_ssl_connect与tls上的隐式ftp

时间:2011-07-05 22:51:02

标签: php ftp ssl ftps

ftp_ssl_connect可以处理TLS上的隐式FTP吗?默认情况下,它使用显式。

我正在尝试上传到服务器,该服务器只接受端口990上的隐式ftp over tls;有没有人遇到这个?你是怎么解决的?

3 个答案:

答案 0 :(得分:15)

ftp_ssl_connect只是明确的

如果您需要隐式,请使用curl

$fp = fopen($path, 'r');
$ftp_server = 'ftps://'.$server.'/'.$filename; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $ftp_server);
curl_setopt($ch, CURLOPT_USERPWD,$user.':'.$pass);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_FTP_SSL, CURLFTPSSL_TRY);
curl_setopt($ch, CURLOPT_FTPSSLAUTH, CURLFTPAUTH_TLS);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);

$output = curl_exec($ch);
$error_no = curl_errno($ch);
//var_dump(curl_error($ch));
curl_close($ch);

答案 1 :(得分:7)

根据frustrum和Steven Jeffries的答案,我进一步扩展了它。这会重用curl连接,并具有一些目录列表功能,包括一个按上次修改后的文件排序。

这适用于PHP 7,对于较低版本,您必须重写< =>操作员线。

sec[2]

答案 2 :(得分:4)

对于碰巧在此页面上谷歌并想要快速解决方案的任何人:

我扩展了frustrum的答案,并使用此方法为基本上传/下载制作了一个简单的类。我希望它有所帮助!

<?php

class ImplicitFtp {

    private $server;
    private $username;
    private $password;

    public function __construct($server, $username, $password) {
        $this->server = $server;
        $this->username = $username;
        $this->password = $password;
    }

    public function download($remote, $local = null) {
        if ($local === null) {
            $local = tempnam('/tmp', 'implicit_ftp');
        }

        if ($fp = fopen($local, 'w')) {
            $ftp_server = 'ftps://' . $this->server . '/' . $remote;
            $ch = curl_init();

            curl_setopt($ch, CURLOPT_URL, $ftp_server);
            curl_setopt($ch, CURLOPT_USERPWD, $this->username . ':' . $this->password);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($ch, CURLOPT_FTP_SSL, CURLFTPSSL_TRY);
            curl_setopt($ch, CURLOPT_FTPSSLAUTH, CURLFTPAUTH_TLS);
            curl_setopt($ch, CURLOPT_UPLOAD, 0);
            curl_setopt($ch, CURLOPT_FILE, $fp);

            curl_exec($ch);

            if (curl_error($ch)) {
                curl_close($ch);
                return false;
            } else {
                curl_close($ch);
                return $local;
            }
        }
        return false;
    }

    public function upload($local, $remote) {
        if ($fp = fopen($local, 'r')) {
            $ftp_server = 'ftps://' . $this->server . '/' . $remote;
            $ch = curl_init();

            curl_setopt($ch, CURLOPT_URL, $ftp_server);
            curl_setopt($ch, CURLOPT_USERPWD, $this->username . ':' . $this->password);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($ch, CURLOPT_FTP_SSL, CURLFTPSSL_TRY);
            curl_setopt($ch, CURLOPT_FTPSSLAUTH, CURLFTPAUTH_TLS);
            curl_setopt($ch, CURLOPT_UPLOAD, 1);
            curl_setopt($ch, CURLOPT_INFILE, $fp);

            curl_exec($ch);
            $err = curl_error($ch);
            curl_close($ch);

            return !$err;
        }
        return false;
    }

}