【问题标题】:ftp_ssl_connect with implicit ftp over tlsftp_ssl_connect 与 tls 上的隐式 ftp
【发布时间】:2011-09-29 04:53:51
【问题描述】:

ftp_ssl_connect 能否通过 TLS 处理隐式 FTP?默认情况下它使用显式。

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

【问题讨论】:

  • IIRC,它不支持隐式。我想我自己也遇到过几次。
  • 您可能想尝试使用 curl 函数。我不确定这是否适用于隐式,但通常那里的选项会给你更多的控制权。
  • 感谢编辑和 cmets
  • 我通过 exec() 运行外部 Perl 脚本(使用 Net::FTPSSL)来修复它,这不是最简洁的解决方案,但如果您乐于混合语言,它可能会有所帮助。

标签: php ftp ssl ftps


【解决方案1】:

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);

【讨论】:

  • 如果需要调试,添加:curl_setopt($ch, CURLOPT_VERBOSE, true);
【解决方案2】:

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

这是针对 PHP 7 的,对于更低版本,您将不得不重写 操作符行。

<?php
/**
 * Implicit FTP 
 * @author Nico
 * Based on
 * http://stackoverflow.com/questions/6589730/ftp-ssl-connect-with-implicit-ftp-over-tls
 * http://stackoverflow.com/questions/845220/get-the-last-modified-date-of-a-remote-file
 */
class ImplicitFtp {

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

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

    public function __destruct() {
        if (!empty($this->curlhandle))
            @curl_close($this->curlhandle);
    }

    /**
     * @param string $remote remote path
     * @return resource a cURL handle on success, false on errors.
     */
    private function common($remote) {
        curl_reset($this->curlhandle);
        curl_setopt($this->curlhandle, CURLOPT_URL, 'ftps://' . $this->server . '/' . $remote);
        curl_setopt($this->curlhandle, CURLOPT_USERPWD, $this->username . ':' . $this->password);
        curl_setopt($this->curlhandle, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($this->curlhandle, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($this->curlhandle, CURLOPT_FTP_SSL, CURLFTPSSL_TRY);
        curl_setopt($this->curlhandle, CURLOPT_FTPSSLAUTH, CURLFTPAUTH_TLS);
        return $this->curlhandle;
    }

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

        if ($fp = fopen($local, 'w')) {
            $this->curlhandle = self::common($remote);
            curl_setopt($this->curlhandle, CURLOPT_UPLOAD, 0);
            curl_setopt($this->curlhandle, CURLOPT_FILE, $fp);

            curl_exec($this->curlhandle);

            if (curl_error($this->curlhandle)) {
                return false;
            } else {
                return $local;
            }
        }
        return false;
    }

    public function upload($local, $remote) {
        if ($fp = fopen($local, 'r')) {
            $this->curlhandle = self::common($remote);
            curl_setopt($this->curlhandle, CURLOPT_UPLOAD, 1);
            curl_setopt($this->curlhandle, CURLOPT_INFILE, $fp);

            curl_exec($this->curlhandle);
            $err = curl_error($this->curlhandle);

            return !$err;
        }
        return false;
    }

    /**
     * Get file/folder names
     * @param string $remote
     * @return string[]
     */
    public function listnames($remote) {
        if (substr($remote, -1) != '/')
            $remote .= '/';
        $this->curlhandle = self::common($remote);
        curl_setopt($this->curlhandle, CURLOPT_UPLOAD, 0);
        curl_setopt($this->curlhandle, CURLOPT_FTPLISTONLY, 1);
        curl_setopt($this->curlhandle, CURLOPT_RETURNTRANSFER, 1);

        $result = curl_exec($this->curlhandle);

        if (curl_error($this->curlhandle)) {
            return false;
        } else {
            $files = explode("\r\n", trim($result));
            return $files;
            return $local;
        }
    }

    /**
     * Get file/folder names ordered by modified date
     * @param string $remote
     * @return string[]
     */
    public function listbydate($remote) {
        $files = $this->listnames($remote);
        if (empty($files))
            return null;
        $filedata = array();
        foreach ($files as $file) {

            $this->curlhandle = self::common($remote . '/' . $file);
            curl_setopt($this->curlhandle, CURLOPT_NOBODY, 1);
            curl_setopt($this->curlhandle, CURLOPT_FILETIME, 1);
            curl_setopt($this->curlhandle, CURLOPT_RETURNTRANSFER, 1);
            $result = curl_exec($this->curlhandle);

            if ($result) {
                $timestamp = curl_getinfo($this->curlhandle, CURLINFO_FILETIME);
                $fileobj = array();
                $fileobj['name'] = $file;
                $fileobj['lastmodified'] = ($timestamp != -1) ? date("Y-m-d H:i:s", $timestamp) : null;
                $filedata[] = $fileobj;
            }
        }

        usort($filedata, function ($item1, $item2) {
            return date($item2['lastmodified']) <=> date($item1['lastmodified']);
        });

        return $filedata;
    }



    /**
     * Get file/folder raw data
     * @param string $remote
     * @return string[]
     */
    public function rawlist($remote) {
        if (substr($remote, -1) != '/')
            $remote .= '/';
        $this->curlhandle = self::common($remote);
        curl_setopt($this->curlhandle, CURLOPT_UPLOAD, 0);
        curl_setopt($this->curlhandle, CURLOPT_RETURNTRANSFER, 1);

        $result = curl_exec($this->curlhandle);

        if (curl_error($this->curlhandle)) {
            return false;
        } else {
            $files = explode("\n", trim($result));
            return $files;
            return $local;
        }
    }

    /**
     * Get file/folder parsed data into an array
     * @param string $remote
     * @return array[]
     */
    public function list($remote) {
        $this->curlhandleildren = $this->rawlist($remote);
        if (!empty($this->curlhandleildren)) {
            $items = array();
            foreach ($this->curlhandleildren as $this->curlhandleild) {
                $chunks = preg_split("/\s+/", $this->curlhandleild);
                list($item['rights'], $item['number'], $item['user'], $item['group'], $item['size'], $item['month'], $item['day'], $item['time']) = $chunks;
                array_splice($chunks, 0, 8);
                $item['name'] = trim(implode(" ", $chunks));
                $item['type'] = $chunks[0]{0} === 'd' ? 'directory' : 'file';
                $items[] = $item;
            }
            return $items;
        }
        return false;
    }

}
?>

【讨论】:

  • 不应该 private function common($remote)private static function common($remote) 使用 PHP 的静态方法运算符 self::common($remote); 调用它?
  • @tonix self:: 允许调用非静态变量 - 请参阅 stackoverflow.com/questions/19218182/…
【解决方案3】:

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

我扩展了 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;
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-23
    • 2019-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-03
    • 1970-01-01
    相关资源
    最近更新 更多