Jump to content

PHP, FTP download each file with For loop i++


mrphobos

Recommended Posts

Greetings all!  ;)I'm writing a php script that will connect to a ftp server and download all the files in the main array. I've got the connection part down. I know how to download specific files and I'm attempting to download ALL the files by listing them in a array and then using a FOR incremental loop to go through one by one and use FTP_get to download each one. I think I'm ALMOST there with this code. It isn't giving me any errors,


<?php
$countfiles = 0;


//Connnect to FTP server
$resource = ftp_connect('idx.fnismls.com');

$login = ftp_login($resource, '*********************', '************');
if($login){
printf("****We are now connected****");
printf("<br>");
}else{
printf("****Connection faild****");
printf("<br>");
}

//Puts file names into an array.
$files = ftp_nlist($resource,"IDX");

//Counts and Gets each file with $files[$i] variable.
for($i = 0; $i < count($files); $i++) {
echo $files[$i];
printf("<br>");
$countfiles++;
echo "$countfiles ...";

//Download section
if (ftp_get($resource, '/var/www/downloads', $files[$i], FTP_BINARY)){
	printf("...File downloaded correctly");
	printf("<br>");
}else{
	printf("...File not downloaded correctly");	
	printf("<br>");
}

}

?>

 

When I take out the download section, the out put will list all the files one by one. If I let it run as is, it will run and run never ending which means the for loop condition of $I <count($files); isn't being met. I simply don't understand why.

 

Any help would be greatly appreciated. I think I'm ALMOST there.

 

The path you want to save to needs to include a file name, try

if (ftp_get($resource, '/var/www/downloads/'.$files[$i], $files[$i], FTP_BINARY)){

 

Also recommend calculating the length of your for loop outside of the loop rather than in it. Or use foreach.

 

 

Hey thanks for your reply. I knew there was some thing vital I was missing. The following is the exact line I'm using

if (ftp_get($resource, '/var/www/downloads/'.$files[$i], $files[$i], FTP_BINARY)){

 

Every thing else is the same. When run, the script returns the same error for every file it tries to download. Theres about 30 of them so I won't copy past the whole thing but here's the first one.

Warning: ftp_get(/var/www/downloads/fileIDX/pics-multi family-20100216.tar) [function.ftp-get]: failed to open stream: No such file or directory in /var/www/gwr/connect.php on line 30

 

It's almost as if it needs the file to already be there before it can replace it. I don't understand what's going on here. Does any one have any suggestions. Thank you for your time.

 

 

 

Hey thanks for your reply. I knew there was some thing vital I was missing. The following is the exact line I'm using

if (ftp_get($resource, '/var/www/downloads/'.$files[$i], $files[$i], FTP_BINARY)){

 

Every thing else is the same. When run, the script returns the same error for every file it tries to download. Theres about 30 of them so I won't copy past the whole thing but here's the first one.

Warning: ftp_get(/var/www/downloads/fileIDX/pics-multi family-20100216.tar) [function.ftp-get]: failed to open stream: No such file or directory in /var/www/gwr/connect.php on line 30

 

It's almost as if it needs the file to already be there before it can replace it. I don't understand what's going on here. Does any one have any suggestions. Thank you for your time.

 

Check your permissions on the directory. Make it 0755 0r 0777

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.