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_mu ltiple_file($se rver, $username, $password, $directory)
{
print "$server<br >";
print "$username<br>" ;
print "$password<br>" ;
print "$directory<br> ";
$location = "ftp://".$server."/".$director y."/";
print "$location<br>" ;
$curl = curl_init();
curl_setopt($cu rl, CURLOPT_URL, $location);
curl_setopt($cu rl, CURLOPT_FTPLIST ONLY, 1);
curl_setopt($cu rl, CURLOPT_USERPWD , "$username:$pas sword");
curl_setopt($cu rl, CURLOPT_RETURNT RANSFER, 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$fetc hfile<br>";
$curl = curl_init();
$file = fopen("$fetchfi le", "w");
curl_setopt($cu rl, CURLOPT_URL, "$location$fetc hfile");
curl_setopt($cu rl, CURLOPT_USERPWD , "$username:$pas sword");
curl_setopt($cu rl, CURLOPT_FILE, $file);
curl_exec($curl );
curl_close ($curl);
fclose($file);
}
}[/PHP]
[PHP]function download_ftp_mu ltiple_file($se rver, $username, $password, $directory)
{
print "$server<br >";
print "$username<br>" ;
print "$password<br>" ;
print "$directory<br> ";
$location = "ftp://".$server."/".$director y."/";
print "$location<br>" ;
$curl = curl_init();
curl_setopt($cu rl, CURLOPT_URL, $location);
curl_setopt($cu rl, CURLOPT_FTPLIST ONLY, 1);
curl_setopt($cu rl, CURLOPT_USERPWD , "$username:$pas sword");
curl_setopt($cu rl, CURLOPT_RETURNT RANSFER, 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$fetc hfile<br>";
$curl = curl_init();
$file = fopen("$fetchfi le", "w");
curl_setopt($cu rl, CURLOPT_URL, "$location$fetc hfile");
curl_setopt($cu rl, CURLOPT_USERPWD , "$username:$pas sword");
curl_setopt($cu rl, CURLOPT_FILE, $file);
curl_exec($curl );
curl_close ($curl);
fclose($file);
}
}[/PHP]