I am trying to upload a file via ftps to a server by doing this:
$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.");
}
What happens it that everything runs fine until the ftp_put() call where I get:
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.
When I do the same via PHP curl calls or even with a stream wrapper (fwrite), everything works fine.
The certificate is fine, the server uses TLS1.3, I tried different contexts but nothing works. In the php source I can see that the error message comes up as PHP tries to use the last session which is NULL.
Can anyone help me out? Is this a basic thing that does not work with ftp_put, maybe due to implicit FTP over TLS?
Update I found out that the file is being created on the server but has 0 bytes.
Update2 After hint in the comments, tried the following code:
$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() );
}
Result was that the file was renamed successfully but a warning came up:
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
It seems this warning can be disabled by telling openssl to ignore this EOF error (https://github.com/php/php-src/issues/8369) so I think I can ignore this for my special case.