09 December, 2009

FTP AND ZIP WITH PHP

You can easily download a file from ftp and archive it using php.
Here is an example for you :

  1. $host = "your ftp host";
  2. $user = "your ftp username";
  3. $pass = "your ftp password";
  4. if (class_exists("ZipArchive"))
  5. {
  6.     $ftp = ftp_connect($host);
  7.     ftp_login($ftp,$user,$pass);
  8.    $zip = new ZipArchive;
  9.    if ($zip->open("test.zip") === true)
  10.    {
  11.         ftp_get($ftp,"eshan.php","index.php",FTP_ASCII);
  12.         $zip->addFile("eshan.php","eshan.php");
  13.         $zip->close();
  14.         unlink("eshan.php");
  15.         echo "ok";
  16.    }
  17.    else {echo "ERROR";}
  18. }
  19. else {echo "Very sad";}
  20. ?>
First we have connected with ftp server and by using ftp_get() function we have downloaded file.
By using addFile() method of ZipArchive class we have added downloaded file.
After that we have deleted downloaded file using unlink() function.

No comments:

Post a Comment