Skip to content

FTP with SSL: ftp_fput(): Connection timed out on successful writes #20240

Closed

Description

@ionutantohi

Description

The following code:

<?php

set_error_handler(function ($severity, $message) {
    echo $message;
});

$host = '';
$user = '';
$pass = ''';

$connection = ftp_ssl_connect($host);
ftp_login($connection, $user, $pass);
ftp_pasv($connection, true);
ftp_fput($connection, 'test-upload.txt', fopen(__DIR__ . '/my-file.txt', 'r'), FTP_BINARY);

Resulted in this output:

ftp_fput(): Connection timed out exception

But even the if exception was raised, the file is written successfully on the server.

But I expected this output instead:

no exception

PHP Version

PHP 8.4.13 (cli) (built: Oct  1 2025 20:33:51) (NTS)
Copyright (c) The PHP Group
Built by Debian
Zend Engine v4.4.13, Copyright (c) Zend Technologies
    with Zend OPcache v8.4.13, Copyright (c), by Zend Technologies

and also on

PHP 8.2.29 (cli) (built: Jul  3 2025 13:07:49) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.29, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.29, Copyright (c), by Zend Technologies

Operating System

Ubuntu 22.04.3 LTS (GNU/Linux 5.15.0-157-generic x86_64)

Activity

  1. ndossche commented on Oct 20, 2025

    @ndossche
    Member

    Please provivde the following extra information:

    • Does this reproduce consistently?
    • Is this a new issue after upgrading PHP versions (i.e. is this a regression)?
    • What FTP server software do you use?
    • How large is the file you try to upload?
    • How long is the timeout?
  2. ionutantohi commented on Oct 20, 2025

    @ionutantohi
    Author

    Does this reproduce consistently? Yes
    Is this a new issue after upgrading PHP versions (i.e. is this a regression)? No. same thing happens on 8.2 and 8.4
    What FTP server software do you use? I will try to find out. ftp_systype returns Windows_NT
    How large is the file you try to upload? small. few kb
    How long is the timeout? I am using timeout defaults. The file is written and exception is thrown in 1-3 seconds

  3. ionutantohi commented on Oct 21, 2025

    @ionutantohi
    Author

    What FTP server software do you use?

    Microsoft Windows Server 2016, running IIS FTP (IIS version 10)

  4. ndossche commented on Oct 23, 2025

    @ndossche
    Member

    I tried reproducing this with no luck. This means that somewhere the poll system call returns 0 before any file descriptors were ready, i.e. in the event of a timeout.
    The default FTP timeout is set to 90 seconds, but you state the file is uploaded in 1-3 seconds. So does the timeout warning appear immediately after the file is uploaded or only after a while (e.g. after 10s of seconds or 90 seconds)?

  5. ionutantohi commented on Oct 24, 2025

    @ionutantohi
    Author

    It appears immediately. check the video.

    I searched if others experienced the same behaviour and I found only 1 occurrence on the laracast forum, but without any solution. https://laracasts.com/discuss/channels/laravel/getting-connection-timed-out-with-flysystem-ftp-even-when-the-file-appears-in-ftp

    Also, despite this behaviour, in the docs is not mentioned that ftp_fput throws an exception, is this expected?

    video.mov

    I am testing this from a vagrant environment with ubuntu 22, but this shouldn't matter because I saw the same behaviour in production using other setup.

    Is there anything for me to enable or change to see more logs for ftp_fput method to help you debug more?

  6. 10 remaining items

  7. ndossche commented on Oct 25, 2025

    @ndossche
    Member

    The strace helped me understand the issue. Thanks.
    The issue is related to the data_available and data_writeable functions in the FTP implementation, which always have a fixed polling time of 1000ms in a loop, but that doesn't play well together with the timeout handling which erroneously triggers if more than a second passed. Makes no sense.
    Unfortunately, the GPT output is completely wrong.
    I'll try to work on a fix.

  8. added a commit that references this issue on Oct 25, 2025
    488a667
  9. ionutantohi commented on Oct 26, 2025

    @ionutantohi
    Author

    Thank you for looking at this!

  10. added 3 commits that reference this issue on Oct 26, 2025
    8761c4e
    6dab33a
    6bcc4e2
  11. added a commit that references this issue on Oct 26, 2025
    64b30ce
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions