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_exec($curl);
curl_close ($curl);
fclose($file);
}
}[/PHP]