FTP get all files

2019-04-28 13:34发布

问题:

I have successfully connected to my FTP using PHP and I can see all the files using: ftp_nlist

But, is there an easy way to then download all these files in the current directory?

I can't see to find any examples of how I'd do this.

Thanks

回答1:

Another simple solution is ....

List the files in an array and download each file individually.

Something like:

$contents = ftp_nlist($conn_id, ".");

foreach ($contents as &$value) { $result = ftp_fget($conn_id, $local,&$value, FTP_BINARY); }

You might need to tweak the code a little...



回答2:

Yes there is. NanoFTPD is an old project from around 2003. It uses PHP to listen on the FTP port and handles all requests from the client. It is able to do all the functionalities including downloading (all) files to whatever directory you want. Take a look here



回答3:

Try using ftp_get()

$local_file = 'filename.txt';
$server_file = 'filename.txt';

$conn_id = ftp_connect($ftp_server);

$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
    echo "Successfully written to $local_file\n";
} else {
    echo "There was a problem\n";
}

ftp_close($conn_id);


标签: php ftp