Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

ftp_get()

Example

Download a file from the FTP server and save it to a local file.

<?php
// connect and login to FTP server
$ftp_server = “ftp.example.com”;
$ftp_conn = ftp_connect($ftp_server) or die(“Could not connect to $ftp_server”);
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);

$local_file = “local.zip”;
$server_file = “server.zip”;

// download server file
if (ftp_get($ftp_conn, $local_file, $server_file, FTP_ASCII))
  {
  echo “Successfully written to $local_file.”;
  }
else
  {
  echo “Error downloading $server_file.”;
  }

// close connection
ftp_close($ftp_conn);
?>

Definition and Usage

The ftp_get() function downloads a file from the FTP server and saves it to a local file.

Syntax

ftp_get(ftp_conn, local_file, server_file, mode, startpos);

Parameter Values

Parameter

Description

ftp_conn

Mandatory. Specifies the FTP connection to use.

local_file

Mandatory. Specifies the local file path (will be overwritten if the file already exists).

server_file

Mandatory. Specifies the file on the server to download.

mode

Optional. Specifies the transfer mode, with possible values being FTP_ASCII or FTP_BINARY.

startpos

Optional. Specifies the starting position in the remote file for downloading.

Technical Details

 

Return Value:

 Optional. Specifies the starting position in the remote file for downloading.

PHP Version:

4+

PHP Changelog:

In PHP 7.3, the mode parameter was made optional.

The startpos parameter was introduced in PHP 4.3.