ftp_ssl_connect 通过 tls 进行隐式 ftp

发布于 2024-11-18 09:40:47 字数 124 浏览 3 评论 0原文

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

我正在尝试上传到端口 990 上仅接受通过 tls 隐式 ftp 的服务器;到目前为止有人遇到过这个吗?你是怎么修好的?

Can ftp_ssl_connect handle Implicit FTP over TLS? By default it uses explicit.

I'm trying to upload to a server that accepts only Implicit ftp over tls on port 990; has anybody run into this as of yet? How did you fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

肩上的翅膀 2024-11-25 09:40:47

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

ftp_ssl_connect is only explicit

if you need implicit, use 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);
悟红尘 2024-11-25 09:40:47

根据平截头体和史蒂文·杰弗里斯的回答,我进一步扩展了它。这重用了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;
    }

}
?>

Based on frustrum and Steven Jeffries's answers I've extended it further. This reuses the curl connection and has some directory listing functions, including one to sort files by last modified.

This is for PHP 7, for lower you will have to rewrite the <=> operator line.

<?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;
    }

}
?>
此生挚爱伱 2024-11-25 09:40:47

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

我扩展了截头体的答案,并使用此方法创建了一个用于基本上传/下载的简单类。我希望它有帮助!

<?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;
    }

}

For anyone who happens to google upon this page and wants a quick solution:

I expanded on frustrum's answer and made a simple class for basic upload/download using this method. I hope it helps!

<?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;
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文