1

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.

4
  • That looks like a hard one to answer. Which version of php do you run exactly? Maybe you can look for the errorstring to find a clue in PHP's sourcecode. github.com/php/php-src and here: heap.space Commented Oct 15, 2024 at 15:11
  • and here I see your error: github.com/php/php-src/blob/master/ext/ftp/ftp.c#L1838 Looks like you lost your session somehow. Commented Oct 15, 2024 at 15:19
  • Yes, I saw that. The "somehow" puzzles me though :) I can't see why that happens. I found out that the file is created and has zero bytes, will add that to the question. Commented Oct 16, 2024 at 7:34
  • Try getting the actual error as seen in this answer Commented Oct 16, 2024 at 7:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.