By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,276 Members | 1,410 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,276 IT Pros & Developers. It's quick & easy.

CURL FTP DOWNLOAD -- Weird..........

P: 14
I am trying to create a generic function I can call to download all files from a single remote FTP directory -- using CURL. I want to multi-thread it, but need to get the single thread functionality working first before I tackle that. Anyway, in my function I can list all the files, but the function I have, no matter how I try, will only return one file -- the last file. My for() loop seems pretty straightforward, so not sure why only the last file is written. Any suggestions or thoughts on why only one file (out of 75 in the FTP directory I'm trying) works? P.S.-- The screen printing is just to see if loops and such are working, so no I won't be returning passwords to the screen when this is working.

[PHP]function download_ftp_multiple_file($server, $username, $password, $directory)

print "$server<br>";
print "$username<br>";
print "$password<br>";
print "$directory<br>";

$location = "ftp://".$server."/".$directory."/";

print "$location<br>";

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $location);
curl_setopt($curl, CURLOPT_FTPLISTONLY, 1);
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$return = trim(curl_exec($curl));
$files = explode("\n", $return);
curl_close ($curl);

$totalfiles = count($files);

print "$totalfiles<br>";

for ($x=0; $x<$totalfiles; $x++)

print "X = $x<br>";

$fetchfile = $files[$x];

print "$fetchfile<br>";
print "$location$fetchfile<br>";

$curl = curl_init();

$file = fopen("$fetchfile", "w");

curl_setopt($curl, CURLOPT_URL, "$location$fetchfile");
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
curl_setopt($curl, CURLOPT_FILE, $file);
curl_close ($curl);



Apr 16 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.