PHP attempting FTPS transfer creates empty files

2019-06-11 10:16发布

问题:

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.

回答1:

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

}


回答2:

The PHP FTP/FTPS Documentations says:

Note: Appending
As of PHP 5.0.0 files may be appended via the ftp:// URL wrapper. In prior versions, attempting to append to a file via ftp:// will result in failure.

Are you sure you are using PHP >= 5.0.0. Or you can try it with FILE_TEXT flag instead of FILE_APPEND.



回答3:

Just a correction to the code in the selected answer:

Lines 11 and 12 should read:

    CURLOPT_URL => $remoteDir,
    CURLOPT_RETURNTRANSFER => 1


标签: php ssl ftp ftps