PHP download entire folder (recursive) via FTP

2019-07-20 22:47发布

问题:

I currently have a very large site approximately 5gb in size with 60,000 files. The current host isn't doing much in the way of helping me transfer the site to the new host and what I was thinking was to make a simply script on my new host to FTP into the old host and download the entire public_html folder (recursively) to the new server. Is this possible and if so, does anyone have any links they could share to aid in this? Much appreciated.

回答1:

There are probably better mechanisms to do what you want to do.

First, can you use sftp or scp from one host to the other?

scp -R username@oldhost:path/to/directory/ /path/to/destination/directory

or

sftp username@oldhost  # then use 'get -r' to download recursively

or

rsync -avz -P username@oldhost:/path/to/directory/ /path/to/destination/directory/

The -P makes it easy to restart a stalled/dead download.

If good tools won't work, then see if wget is installed:

wget --mirror --continue --ftp-user=username ftp://oldhost/path/to/directory/

The --continue makes it easier to restart a stalled/dead download.



回答2:

If there are a lot of files, I strongly recomend you to make a .tar.gz archive. I don't know what restrictions to php do you have but you can try this one in php:

$archive = "backup.tar.gz";
$directory = "./www";
exec( "tar -czf $archive $directory");

Then you can simply download 1 single gziped archive via http / ftp or using any other method.



回答3:

Yes you can do it in pure PHP !

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);

If you want to look at the code, you will see recursive functions that do the magic ;)