ftp_get() function in PHP


The ftp_get() function is used to downloads file from the FTP server and save it into a local file.

Syntax

ftp_fget(con,local_file,server_file,mode,startpos);

Parameters

  • con − The FTP connection

  • local_file − A file where the data is stored

  • server_file − The server file to download

  • mode − The transfer mode

  • startpos − The position to begin downloading from. Added in PHP 4.3.0.

Return

The ftp_fget() function returns TRUE on success and FALSE on failure.

Example

The following is an example wherein we will download server file “demo.txt” and save it to local file “new.txt” −

<?php
   $ftp_server="192.168.0.4";
   $ftp_user="amit";
   $ftp_pass="tywg61gh";
   $con = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
   $login = ftp_login($con, $ftp_user, $ftp_pass);
   $my_serverfile = "demo.txt";
   $my_file = "new.txt";
   if (ftp_fget($con, $my_file, $my_serverfile, FTP_ASCII, 0)) {
      echo "Written to local file!";
   } else {
      echo "Error in downloading the server file!";
   }
   ftp_close($con);
   fclose($file_pointer);
?>

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

184 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements