PHP Ftp Multiple Files?

rsquare

Newbie
Joined
Jul 16, 2008
Messages
31
Reaction score
39
I use the following PHP script fragment to DL one file to my server and it works great. But I would like to DL multiple files from the same server. I would like to be able to call to a txt file with a list of the files and then download each in turn to the server. Any help, suggestions, or sample code would be appreciated.

Code:
$ftp_server = "server.com";
$ftp_user_name = "name";
$ftp_user_pass = "password";
$source_file = "/dir/file1.zip";
$destination_file = "/home/public_html/dl/file1.zip";
 
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
 
if ((!$conn_id) || (!$login_result)) {
     echo "FTP connection has failed to $ftp_server as $ftp_user_name.<br />";
     exit;
} else {
     echo "Connected to $ftp_server successfully as $ftp_user_name.<br />";
}
 
$download = ftp_get($conn_id, $destination_file, $source_file, FTP_BINARY);
 
if (!$download) {
     echo "FTP download has failed!";
} else {
     echo "Downloaded $source_file from $ftp_server as $destination_file";
}
fclose($fp);
 
You can use foreach loop to upload or download multiple files from your server or to your server. You Can use same code with the loop.

Hope it helps
 
yup exactly just use the loop.. here's an example :
PHP:
//asume you put each filename per line on list-file.txt
$f = fopen("list-file.txt", "r");
while ( $lines = fgets($f, 1024)) {
	$filename = $lines;
             $destination_file = "/home/public_html/dl/$filename";
             myftpfunction($destination_file);
}
fclose($f);


function myftpfunction($destination_file){
//your ftp code goes here
}
 
I am following you, but how do I reference the $source_file for the multiple files that I want to DL from the server?
 
Check out

http://aziz.oraij.com/

He has a great class for doing this sort of stuff..
 
Check out

http://aziz.oraij.com/

He has a great php class for doing bulk FTPing in PHP
 
Back
Top