【问题标题】:PHP attempting FTPS transfer creates empty filesPHP 尝试 FTPS 传输会创建空文件
【发布时间】:2010-08-30 04:28:06
【问题描述】:

我目前正在尝试使用 PHP 在我们的服务器和远程 FTPS(基于 SSL 的 FTP)服务器之间传输小文件。我是完成这项工作的标准公平,即 file_put_contents、file_get_contents 等......具有以下流上下文:

stream_context_create(array('ftp' => array('overwrite' => true), 'ssl' => array('allow_self_signed' => true)))

我正在使用以下代码传递此上下文流。它可以很好地连接到 FTPS 服务器,但是在创建远程文件时,文件本身完全是空的。文件大小为 0 时为空。

    if(false === file_exists($localFile))
    {
        throw new Exception("Local file, {$localFile}, does not exist.");
    }

    if(false === $localFileContents = file_get_contents($localFile))
    {
        throw new Exception("Could not open Local file, {$localFile}.");
    }

    if(false === file_put_contents("{$this->url}{$remoteFile}", $localFileContents, FILE_APPEND, $this->context))
    {
        throw new Exception("Could not write to remote file, {$remoteFile}.");
    }

远程文件位置,即$this->url,格式如下:“ftps://{user}:{pass}@{host}:{port}”

我们目前使用的是 Windows/Apache 设置,所以如果不编译我们自己的 PHP 二进制文件,我就无法使用 ftp_ssl_connect()。无论如何,我们不能走这条路,因为这是我们环境的重大变化。

【问题讨论】:

    标签: php ssl ftp ftps


    【解决方案1】:

    我只是不得不做一些非常相似的事情。

    我在这里找到了解决方案:http://www.php.net/manual/en/function.curl-setopt.php#90988

    我最终将它包装在一个类中:

    class ftps {
    
        /**
         * @param string $remoteDir Fully quantified path to the directory, eg ftp://foo:bar@blergh.com/directory/
         */
        public function ls($remoteDir) {
    
            $connection = $this->initConnection();
    
            curl_setopt_array($connection, array(
                CURLOPT_URL => $remoteDir,
                CURLOPT_RETURNTRANSFER => 1
            ));
    
            $result = curl_exec($connection);
    
            $this->finishConnection($connection);
    
            return explode("\n", $result);
    
        }
    
        private function initConnection()
        {
            $connection = curl_init();
    
            curl_setopt_array($connection, array(
                CURLOPT_SSL_VERIFYPEER => false,
                CURLOPT_SSL_VERIFYHOST => false,
                CURLOPT_FTP_SSL => CURLFTPSSL_TRY
            ));
    
            return $connection;
        }
    
        private function finishConnection(&$connection)
        {
            curl_close($connection);
            unset($connection);
        }
    
    }
    

    【讨论】:

    • 永远不要这样做。通过禁用对等和主机验证,您完全使加密无效,因为任何关心的人都可以使用中间人攻击来读取您的传输。如果您打算以这种方式禁用验证,请不要使用 SSL/TLS。
    • 同意。如果您无法判断您的流量没有被拦截,那么它主要会使 SSL 失效。如果您无法控制远程服务器,您知道如何克服 OPs 问题吗?我很想知道。
    • 不看 OP 的实际代码是不可能知道的。发布的 sn-p 不完整。它也无法在其流上下文中验证对等方,因此它并不比使用 curl 禁用验证更安全。
    【解决方案2】:

    PHP FTP/FTPS Documentations 说:

    注意:附加
    从 PHP 5.0.0 开始,可以通过 ftp:// URL 包装器附加文件。在以前的版本中,尝试通过 ftp:// 附加到文件将导致失败。

    您确定您使用的是 PHP >= 5.0.0.或者您可以使用FILE_TEXT 标志而不是FILE_APPEND 进行尝试。

    【讨论】:

      【解决方案3】:

      只是对所选答案中的代码进行更正:

      第 11 行和第 12 行应为:

          CURLOPT_URL => $remoteDir,
          CURLOPT_RETURNTRANSFER => 1
      

      【讨论】:

      • 不客气!当我在搜索这个时,是谷歌把我带到了这里:)
      • 谢谢,已在答案中解决了这个问题。
      猜你喜欢
      • 2021-03-10
      • 2013-07-11
      • 1970-01-01
      • 1970-01-01
      • 2021-12-21
      • 1970-01-01
      • 1970-01-01
      • 2017-04-08
      • 1970-01-01
      相关资源
      最近更新 更多