尝试FTPS传输的PHP会创建空文件 [英] PHP attempting FTPS transfer creates empty files

查看:153
本文介绍了尝试FTPS传输的PHP会创建空文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正尝试使用PHP在我们的服务器和远程FTPS(FTP over SSL)服务器之间传输小文件。我是完成这个任务的标准集合,即file_put_contents,file_get_contents等等,以及下面的流上下文:

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

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

  if(false === file_exists($ localFile))
{
抛出新的异常(本地文件,{$ localFile},不存在。);


if(false === $ localFileContents = file_get_contents($ localFile))
{
抛出新异常(Could not open Local file,{$ LOCALFILE})。
}

if(false === file_put_contents({$ this-> url} {$ remoteFile},$ localFileContents,FILE_APPEND,$ this-> context))
{
抛出新的异常(无法写入远程文件,{$ remoteFile}。);
}

远程文件位置,即$ this-> url,位于以下format:ftps:// {user}:{pass} @ {host}:{port}



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

非常相似。



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



  class ftps {

/ **
* @param string $ remoteDir完全量化目录的路径,例如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(\\\
,$ result);


$ b私有函数initConnection()
{
$ connection = curl_init();

curl_setopt_array($ connection,array(
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_FTP_SSL => CURLFTPSSL_TRY
)) ;

返回$ connection;
}

private function finishConnection(& $ connection)
{
curl_close($ connection);
unset($ connection);
}

}


I'm currently attempting to use PHP to transfer small files between our server and a remote FTPS (FTP over SSL) server. I'm the standard fair to get this done, ie, file_put_contents, file_get_contents, etc... with the following stream context:

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

I'm passing this context stream using the following code. It can connect to the FTPS server just fine, but when it comes to creating the remote file, the file itself completely empty. Empty as in 0 for the file size.

    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}.");
    }

The remote file location, ie $this->url, is in the following format: "ftps://{user}:{pass}@{host}:{port}"

We're currently using Windows/Apache setup, so I can't use ftp_ssl_connect() without compiling our own PHP binaries. We couldn't go this route anyhow as it's a major change in our environment.

解决方案

I've just had to do something very similiar.

I found the solution here: http://www.php.net/manual/en/function.curl-setopt.php#90988

I ended up wrapping it in a class thusly:

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

}

这篇关于尝试FTPS传输的PHP会创建空文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆