Exploring the FTP functions

I’m still drowning in the work, and exams are coming close too, but i decided to blog something about the FTP functions in PHP. The script will download all the files that are available on the remote server.

// 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);

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>