PHP FTP functions slow?

Associate
Joined
18 Feb 2008
Posts
1,050
I'm trying to clone an FTP folder using PHP. I haven't used PHP for quite a while so I might be doing something wrong.

Just listing an FTP folder with ~900 files in about 20 directories takes 2-3 minutes :confused:. Can anyone see a problem or are the ftp_* functions in PHP just really slow:

PHP:
<?php
set_time_limit(0);

// FTP details omitted :p

function ftp_recursive_list($resource, $dir)
{
    $files = ftp_nlist($resource, $dir);
    foreach ($files as $file)
    {
        if ($file == '.' || $file == '..') continue;

        $fullPath = $file;
        if (!empty($dir) && !in_array($dir, array('.', '/')))
        {
            $fullPath = $dir . '/' . $fullPath;
        }
        $fileSize = ftp_size($resource, $fullPath);

        // Just outputting file/size instead of copying while testing
        printf("%s (%d)<br />", $fullPath, $fileSize);

        if ($fileSize == -1) ftp_recursive_list($resource, $fullPath);
    }
}

$handle = ftp_connect($host, $port);
if ($handle)
{
    if (!ftp_login($handle, $user, $pass))
    {
        ftp_close($handle);
        die("Invalid Login");
    }
    // Tried with and without passive
    ftp_pasv($handle, true);

    ftp_recursive_list($handle, $dir);
    ftp_close($handle);
}

?>

FTP clients can list all the files almost instantly, and wget can clone the entire FTP folder in around 30 seconds.

Thanks in advance for any help.
 
Back
Top Bottom