PHP: ftp_login fails with "failed to create the SSL context"
I'm trying开发者_运维技巧 to upload a file to a remote FTP server (which requires FTPES) using PHP. The script I've written works locally, but on the live server ftp_login() returns false and the following warnings appear in the error log:
PHP Warning: ftp_login(): failed to create the SSL context [...]
PHP Warning: ftp_login(): AUTH command ok; starting SSL connection. [...]
I know that the login details are correct (since identical code works locally). I can successfully connect to the FTP server from the live server using curl on the command line.
The server is running PHP 5.3.3 (Zend Server on CentOS). I can see from phpinfo that the PHP configure command includes -with-openssl=/usr/local/openssl-0.9.8o
The code is simply this:
$ftpConnection = ftp_ssl_connect('hostname');
if (!$ftpConnection) {
echo "Failed to connect to FTP Site\n";
return false;
}
if (!ftp_login($ftpConnection, 'xxxxx', 'xxxxx')) {
echo "Failed to login to FTP site\n";
return false;
}
For reference my local box (where this works fine) is running PHP 5.3.3-1ubuntu9.3.
Can anyone point me in the right direction?
Edit: I've noticed that the SSL certificate for this server isn't actually valid as the hostname I'm connecting to does not match the common name on the cert. Is there a PHP setting somewhere which controls how strict it or openSSL is regarding certificate errors? That might be the only issue.
I eventually solved this problem by changing the code to use the PHP cURL functions instead, since I knew I could connect okay from the command line with that. There probably is way to get this working with the FTP functions, but in case this helps anyone else here's my working cURL version:
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => 'ftp://username:password@hostname/path/to/file'
CURLOPT_UPLOAD => 1,
CURLOPT_INFILE => $fp,
CURLOPT_INFILESIZE => $localFileSize,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_FTP_SSL => CURLFTPSSL_TRY,
CURLOPT_VERBOSE => true
));
if (curl_exec($ch)) {
curl_close($ch);
}
精彩评论