Mr J
Mr J

Reputation: 2965

PHP FTP library/function to download whole directory and sub directories to local drive

Does anyone know of a PHP FTP library/function that is capable of downloading whole directories to the local drive. I've tried to write a function to do this but its beyond me :-(

I cant use secure copy or anything like that it has to be a PHP script ran from a local machine to log into a remote server and download a specified directory.

Thanks!

Upvotes: 3

Views: 3543

Answers (3)

Shakir El Amrani
Shakir El Amrani

Reputation: 330

You can do this very easily by using this library :

The code :

$connection = new FtpConnection('host', 'username', 'password');

$client = new FtpClient($connection);

if (asyncDownload('yourLocalFolder', '.')) {
    echo 'Done!';
}

function syncDownload($localDir, $remoteDir)
{
    global $client;
    
    if (!is_dir($localDir)) {
        mkdir($localDir);
    }
    
    /**
     * listDirectoryDetails method will recursively gets all the files with
     * their details within the giving directory.
     */
    $files = $client->listDirectoryDetails($dir, true);
    
    foreach($files as $file) {
        $client->download($file['path'], $localDir . '/' . $file['name'], true, FtpWrapper::BINARY);
    }
    
    return true;
}

Upvotes: 0

hugsbrugs
hugsbrugs

Reputation: 3611

I've just released 2 new libraries to do such things in FTP / SFTP

Recursively copy files and folders on remote SFTP server (If local_path ends with a slash upload folder content otherwise upload folder itself)

Ftp::upload_dir($server, $user, $password, $local_path, $remote_path, $port = 22);

Download a directory from remote FTP server (If remote_dir ends with a slash download folder content otherwise download folder itself)

Ftp::download_dir($server, $user, $password, $remote_dir, $local_dir, 

$port = 22);

Upvotes: 0

djdy
djdy

Reputation: 6919

http://php.net/manual/en/function.ftp-get.php

Look in the comments: mroerick at gmx dot net 15-May-2009 07:42

Idea is to: login > get list of files > download the files, as the example does.

Upvotes: 2

Related Questions