PHP ftp_put()上传FTPS文件失败:无法获取SSL会话求助
我用以下代码尝试通过FTPS向服务器上传文件:
$connectionId = ftp_ssl_connect($server, 21, 15); if ($connectionId === false) { throw new Exception("Could not connect to server via FTPs."); } echo "Connected".PHP_EOL; if (!ftp_login($connectionId, $username, $password)) { throw new Exception("Unable to authenticate with server."); } echo "Login".PHP_EOL; if (!ftp_pasv($connectionId, true)) { throw new Exception("Could not turn on passive mode."); } echo "Passive".PHP_EOL; $path = '.'; if (!ftp_chdir($connectionId, $path)) { throw new Exception("Could not change to directory ".$path.". Current directory :".ftp_pwd($connectionId)); } echo "Chdir".PHP_EOL; $filename = './test.txt'; if (!ftp_put($connectionId, basename($filename), $filename)) { throw new Exception("Could not upload file to server."); }
执行流程一切正常,但调用ftp_put()时出现以下错误:
PHP Warning: ftp_put(): data_accept: failed to retrieve the existing SSL session in ...
PHP Warning: ftp_put(): Accepted data connection in...
PHP Fatal error: Uncaught Exception: Could not upload file to server.
但用PHP curl或者流包装器(fwrite)上传完全正常,证书没问题,服务器用的是TLS1.3,我试过不同上下文都没用。查看PHP源码发现,这个错误是因为PHP尝试使用的最后一个会话为NULL导致的。
请问这是ftp_put本身的问题吗?比如和隐式FTPS有关?有没有解决办法?
更新:发现服务器上已经创建了文件,但大小是0字节。
更新2:根据提示我试了这段代码:
$filename = './test.txt'; $filenameNew = './test_new.txt'; if (ftp_rename($connectionId, $filename, $filenameNew)) { echo "successfully renamed $filename to $filenameNew\n"; } else { echo "There was a problem while renaming $filename to $filenameNew\n"; print_r( error_get_last() ); }
结果文件重命名成功,但出现警告:
successfully renamed ./test.txt to ./test_new.txt
PHP Warning: PHP Request Shutdown: SSL_read on shutdown: error:0A000126:SSL routines::unexpected eof while reading in Unknown on line 0
看起来可以通过让openssl忽略此EOF错误来禁用该警告,所以我觉得这个警告可以在我的场景中忽略。
内容的提问来源于stack exchange,提问作者4thfloorstudios

