PHP FTP 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
User avatar
legend986
Forum Contributor
Posts: 258
Joined: Sun Jul 15, 2007 2:45 pm

PHP FTP help

Post by legend986 »

1. I'm trying to fetch a large file from another ftp server onto my local server. For files larger than say 30 MB, the PHP Timeout will occur. So is there a way I could actually pause the transfer, refresh the php file and resume it to complete the whole transfer?

2. I could transfer a file from one server to another server. How would I do the same for a directory?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

1) set_time_limit()

2) There is no FTP function to retrieve an entire directory. If your FTP client has a command window & you watch closely, you'll see that the client opens a directory, does a listing of all the files in that directory, then downloads the files individually. You'll have to do that too.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
legend986
Forum Contributor
Posts: 258
Joined: Sun Jul 15, 2007 2:45 pm

Post by legend986 »

1. Thank you... One doubt however, I read it on the website, but what if max_execution_time is defined by the host? Will that be ignored?

2. In that case, I need to fetch each and every file name right?

I am getting the listing using

Code: Select all

$list = ftp_rawlist($resource, '/public_html');
but how would I fetch the file names from that to be able to transfer?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Have a look at the ftp_nlist function...

Here's an example:

Code: Select all

// make sure we have time enough to execute this script
set_time_limit(1200);

// connect to the ftp server
$ftp = ftp_connect(‘ftp.scarlet.be’);
ftp_login($ftp, ‘anonymous’, ‘password’);

// get the files that are available here
$local = glob(‘*.*’);

// get the files that are available there
$remote = ftp_nlist($ftp, ‘.’);

// get the files there that are not availble here
foreach($remote as $file)
{
  if (!in_array($file, $local))
  {
    // we don’t have the file, thus download it
    ftp_get($ftp, $file, $file, FTP_BINARY);
  }
}

// close the connection
ftp_close($ftp);
Post Reply