ftp_get not working with large file

2019-08-03 22:57发布

This is my code:

$con = ftp_connect('ftpserver.com');
ftp_login($con, 'username', 'password');
ftp_pasv($con, true);
ftp_set_option($con, FTP_TIMEOUT_SEC, 180);
ftp_get($con, './test.txt', '/path/to/file.txt', FTP_ASCII);
ftp_close($con);

I'm getting this error after the script runs for more than a few minutes:

PHP Warning: ftp_get(): Opening ASCII mode data connection for /path/to/file.txt (6137508 bytes). In /path/to/script.php

When it's running I can see the test.txt file is created and even some data shows up in it but it very abruptly stops downloading at about 23,135 bytes. After the script throws the warning test.txt gets removed.

UPDATE

I came up with a work around.

function getFile() {
    $fh = fopen('test.txt', 'a');

    $con = ftp_connect('ftpserver.com');
    ftp_login($con, 'username', 'password');
    ftp_pasv($con, true);
    ftp_set_option($con, FTP_TIMEOUT_SEC, 1);

    if (file_exists('test.txt')) {
        $size  = ftp_size($con, '/path/to/file.txt');
        $size2 = filesize('test.txt');
        @ftp_fget($con, $fh, '/path/to/file.txt', FTP_ASCII, $size2);
        fclose($fh);
        clearstatcache();
        if (filesize('test.txt') !== $size) {
            getFile();
        }
    }
    else
        @ftp_fget($con, $fh, '/path/to/file.txt', FTP_ASCII);

    ftp_close($con);
}

getFile();

标签: php ftp
0条回答
登录 后发表回答