PHP FTP function help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tpae
Forum Newbie
Posts: 3
Joined: Sun Apr 12, 2009 1:12 pm

PHP FTP function help

Post by tpae »

Hello,

I need to figure out how to connect to a remote ftp server through php, and then downloading all the contents to the local server.

Only method I could think of is to keep using ftp_nlist for each subfolder and download each folder with a big loop. I guess when ftp_nlist returns 0, go back a directory and keep doing the same thing...

I am very lost how to begin...
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: PHP FTP function help

Post by it2051229 »

yes it is possible...

first step is you can make use of the function "ftp_rawlist(ftpConnection, directoryPath, true)". The function is provided by PHP and you see the "true" value over there? it means it will get all the files and folders inside the given directory path(including sub directories)...

take note that the result will be an array which contains files and directories... in my case, when I looped on the array of files and directories this is what i got:

Code: Select all

 
-rw-r--r-- 1 4403 vhosts 1386 Jan 30 00:01 CalendarEventsAjaxHandler.php
-rw-r--r-- 1 4403 vhosts 3892 Jul 21 2008 CalendarEventsFormsHandler.php
 
includes/generics:
 
-rw-r--r-- 1 4403 vhosts 19475 Oct 2 2008 Archive.php
-rw-r--r-- 1 4403 vhosts 1246 Jun 16 2008 DatabaseConnector.php
 
includes/generics/font:
 
-rw-r--r-- 1 4403 vhosts 250 Aug 19 2008 courier.php
-rw-r--r-- 1 4403 vhosts 47 Aug 19 2008 desktop.ini
 
then all you have to do now is to simply parse each line so that each file will be something like :

Code: Select all

 
CalendarEventsAjaxHandler.php
CalendarEventsFormsHandler.php
includes/generics/Archive.php
includes/generics/DatabaseConnector.php
includes/generics/font/courier.php
includes/generics/font/desktop.ini
 
And then after parsing the files.. you can make use of the function "ftp_get(filePath)" using a loop so that PHP will download each of every file which was parsed awhile ago...

You can google the ftp_get and ftp_rawlist for more details.
tpae
Forum Newbie
Posts: 3
Joined: Sun Apr 12, 2009 1:12 pm

Re: PHP FTP function help

Post by tpae »

I did it, but only problem right now is when the file structure is too big, it hangs and throws this error:

Fatal error: Maximum execution time of 30 seconds exceeded in D:\www\www\_beta\ftp.php on line 34
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: PHP FTP function help

Post by it2051229 »

that's because your PHP was set up to execute to 30 seconds only on one page... in order to adjust 30 seconds.. you visit the .ini file of your php and then find the max execution time and change it to whatever seconds that you want to.
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: PHP FTP function help

Post by it2051229 »

i'm, just curious, does it have to be REMOTE_FTP? cause i found a PHP function that will ZIP the whole Directory and then all you have to do is download that ZIPPED directory not via FTP but just like a normal download session(well at least your 30 second problem is over).
tpae
Forum Newbie
Posts: 3
Joined: Sun Apr 12, 2009 1:12 pm

Re: PHP FTP function help

Post by tpae »

Well, heres my script:

downloads the FTP content from client's server to my hosting server, so he/she doesn't need to worry about transferring it on their own.

I wanted to make an automated feature which allows me to do that.

I really appreciate your help! I think I figured out the 30 second limit.

Thank you! :)
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: PHP FTP function help

Post by it2051229 »

you know transferring bulk of files one by one takes a lot of time...

just my suggestion:

it would be better to automatically zip the whole directory so that it will be just one file.. and that one zipped file is transferred to the hosting server which is more faster actually rather than doing it one by one for each file. Then when the zipped file is totally transferred to the hosting server, it will be automatically decompressed. So you see, you don't need parsing anymore just to get all the file and there directory... but anyways, you've already made it.. sooo yeah, it's just a suggestion.
Post Reply