mrphobos Posted February 22, 2010 Share Posted February 22, 2010 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 https://forums.phpfreaks.com/topic/193006-php-ftp-download-each-file-with-for-loop-i/ Share on other sites More sharing options...
KrisNz Posted February 23, 2010 Share Posted February 23, 2010 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 https://forums.phpfreaks.com/topic/193006-php-ftp-download-each-file-with-for-loop-i/#findComment-1016546 Share on other sites More sharing options...
mrphobos Posted February 23, 2010 Author Share Posted February 23, 2010 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 https://forums.phpfreaks.com/topic/193006-php-ftp-download-each-file-with-for-loop-i/#findComment-1017016 Share on other sites More sharing options...
scarhand Posted February 23, 2010 Share Posted February 23, 2010 Its telling you the problem. The file/directory you're specifying does not exist, so make sure it exists by checking the path you enter on your server. Link to comment https://forums.phpfreaks.com/topic/193006-php-ftp-download-each-file-with-for-loop-i/#findComment-1017046 Share on other sites More sharing options...
$Three3 Posted February 23, 2010 Share Posted February 23, 2010 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 https://forums.phpfreaks.com/topic/193006-php-ftp-download-each-file-with-for-loop-i/#findComment-1017062 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.