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.

 

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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.

 

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.