Upload Download Files Through FTP In PHP (Simple Examples)

Welcome to a quick tutorial on how to upload and download files through FTP using PHP. Need to manage some files on a server through an automated script?

To upload and download files using PHP FTP, we only need to enable extension=ftp in php.ini and use it accordingly:

  • $ftp = ftp_connect("HOST");
  • ftp_login($ftp, "USER", "PASSWORD");
  • Download files: ftp_get($ftp, "DESTINATION", "SOURCE", FTP_BINARY);
  • Upload files: ftp_put($ftp, "DESTINATION", "SOURCE", FTP_BINARY);
  • ftp_close($ftp);

That should cover the basics, but let us walk through a few actual examples in this guide – Read on!

 

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

EXAMPLE CODE DOWNLOAD

Source code on GitHub Gist

Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

SORRY FOR THE ADS...

But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.

Buy Me A Coffee Code Boxx eBooks

 

 

FTP UPLOAD & DOWNLOAD IN PHP

All right, let us now get into the examples of how to upload and download files through FTP in PHP.

 

PART 1) PHP FTP EXTENSION

1A) DOWNLOAD FILES WITH PHP FTP 

1a-ftp-download.php
<?php
// (A) FTP SETTINGS - CHANGE TO YOUR OWN!
$ftphost = "ftp.example.com";
$ftpuser = "user";
$ftppass = "password";
$source = "file.txt"; // target file on ftp server
$destination = "downloaded.txt"; // save to this file
 
// (B) CONNECT TO FTP SERVER
$ftp = ftp_connect($ftphost) or exit("Failed to connect to $ftphost");
 
// (C) LOGIN & DOWNLOAD
if (ftp_login($ftp, $ftpuser, $ftppass)) {
  echo ftp_get($ftp, $destination, $source, FTP_BINARY)
    ? "Saved to $destination"
    : "Error downloading $source" ;
} else { echo "Invalid user/password"; }
 
// (D) CLOSE FTP CONNECTION
ftp_close($ftp);

In this first section, we will walk through how to use the “legit” PHP FTP extension. This should be very straightforward and easy to understand.

  • We use the ftp_connect() function to connect to an FTP server.
  • Next, log in to the FTP server using the ftp_login() function.
  • Then download the required files using ftp_get().
  • Finally, remember to close the FTP connection using ftp_close().

 

 

1B) UPLOAD FILES WITH PHP FTP 

1b-ftp-upload.php
<?php
// (A) FTP SETTINGS - CHANGE TO YOUR OWN!
$ftphost = "ftp.example.com";
$ftpuser = "user";
$ftppass = "password";
$source = "file.txt"; // source file on server
$destination = "uploaded.txt"; // save to this file on ftp server
 
// (B) CONNECT TO FTP SERVER
$ftp = ftp_connect($ftphost) or exit("Failed to connect to $ftphost");
 
// (C) LOGIN & UPLOAD
if (ftp_login($ftp, $ftpuser, $ftppass)) {
  echo ftp_put($ftp, $destination, $source, FTP_BINARY)
    ? "Uploaded to $destination"
    : "Error uploading $source" ;
} else { echo "Invalid user/password"; }
 
// (D) CLOSE FTP CONNECTION
ftp_close($ftp);

That’s right, this is pretty much the same. Except that to upload files, we use ftp_put() instead.

 

 

1C) OTHER PHP FTP COMMANDS

1c-ftp-commands.php
<?php
// (A) FTP SETTINGS - CHANGE TO YOUR OWN!
$ftphost = "ftp.example.com";
$ftpuser = "user";
$ftppass = "password";
 
// (B) CONNECT TO FTP SERVER
$ftp = ftp_connect($ftphost) or exit("Failed to connect to $ftphost");
 
// (C) LOGIN & FTP YOGA
if (ftp_login($ftp, $ftpuser, $ftppass)) {
  $currentDir = ftp_pwd($ftp); // fet current folder
  $files = ftp_nlist($ftp, $currentDir); // list files & folders
  $ok = ftp_chdir($ftp, "FOLDER"); // change the current folder
  print_r($files);
} else { echo "Invalid user/password"; }
 
// (D) CLOSE FTP CONNECTION
ftp_close($ftp);

Yes, it is also possible to get the current folder, change the folder, or get the list of files on the FTP server.

 

PART 2) FTP WITH PHP-CURL

2A) DOWNLOAD FILES WITH CURL FTP

2a-curl-download.php
<?php
// (A) FTP SETTINGS - CHANGE TO YOUR OWN!
$ftphost = "ftp://example.com";
$ftpuser = "user";
$ftppass = "password";
$source = "FILE.txt"; // file to download on ftp server
$destination = "DOWNLOAD.txt"; // file to save locally
 
// (B) INIT CURL + OPEN LOCAL FILE
$curl = curl_init();
$file = fopen($destination, "w");
 
// (C) SET CURL OPTIONS
curl_setopt_array($curl, [
  CURLOPT_URL => $ftphost . $source,
  CURLOPT_USERPWD => "$ftpuser:$ftppass",
  CURLOPT_RETURNTRANSFER => 1,
  CURLOPT_FILE => $file
]);

// (D) EXECUTE CURL (DOWNLOAD FILE)
curl_exec($curl);
 
// (E) CLOSE CONNECTION + FILE
curl_close($curl);
fclose($file);

If you somehow cannot get PHP FTP to work properly, this is an alternative way of using PHP cURL to work with FTP. Downloading a file through FTP cURL is a little more complicated, but still straightforward nonetheless:

  • We initialize cURL with curl_init().
  • Open and create an empty file on the server with fopen().
  • Set the cURL options with curl_setopt_array:
    • CURLOPT_URL Address of the FTP server
    • CURLOPT_USERPWD User and password.
    • CURLOPT_RETURNTRANSFER Set to return transfer, because we are downloading stuff.
    • CURLOPT_FILE Pointing this to the empty file we opened with fopen().
  • Execute cURL with curl_exec(), watch it do the magic.
  • Finally, remember to close the connection with curl_close(), and also the file with fclose().

 

 

2B) UPLOAD FILES WITH CURL FTP

2b-curl-upload.php
<?php
// (A) FTP SETTINGS - CHANGE TO YOUR OWN!
$ftphost = "ftp://example.com";
$ftpuser = "user";
$ftppass = "password";
$source = "FILE.txt"; // file to upload to ftp server
$destination = "UPLOAD.txt"; // file name on ftp server
 
// (B) INIT CURL + OPEN LOCAL FILE
$curl = curl_init();
$file = fopen($source, "r");
 
// (C) SET CURL OPTIONS
curl_setopt_array($curl, [
  CURLOPT_URL => $ftphost . $destination,
  CURLOPT_USERPWD => "$ftpuser:$ftppass",
  CURLOPT_UPLOAD => 1,
  CURLOPT_INFILE => $file,
  CURLOPT_INFILESIZE => filesize($source)
]);
 
// (D) EXECUTE CURL (UPLOAD FILE)
curl_exec($curl);
 
// (E) CLOSE CONNECTION + FILE
curl_close($curl);
fclose($file);

Uploading is pretty much the opposite of downloading. Instead of creating an empty file on the server, we read from an existing file instead.

 

 

EXTRAS

That’s all for this tutorial, and here is a small section on some extras that may be useful to you.

 

PHP FTP – A QUICK SUMMARY

Function Description
ftp_connect(URL) Connects to target FTP host.
ftp_login(STREAM, USER, PASSWORD) Login to the FTP server.
ftp_get(STREAM, DESTINATION, SOURCE, MODE) Downloads selected file.
ftp_put(STREAM, DESTINATION, SOURCE, MODE) Uploads selected file.
ftp_close(STREAM) Closes FTP connection.

 

PHP CURL FTP – A QUICK SUMMARY

Function Description
curl_init() Initialize cURL connection.
fopen(FILE, MODE) Opens a file for reading or writing.
curl_setopt_array(STREAM, ARRAY) Sets cURL options.

  • CURLOPT_URL: Host URL
  • CURLOPT_USERPWD: The user and password (user:password).
  • CURLOPT_RETURNTRANSFER: Returns transfer from the server? True or false.
  • CURLOPT_FILE: Target file to save to (download).
  • CURLOPT_UPLOAD: Has file upload? True or false.
  • CURLOPT_INFILE: Target file to upload.
  • CURLOPT_INFILESIZE: Size of file to upload.
curl_exec(STREAM) Executes cURL.
curl_close(STREAM) Closes cURL connection.
fclose(FILE) Commits and writes file properly.

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end of this guide. I hope that it has helped you with your project, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

3 thoughts on “Upload Download Files Through FTP In PHP (Simple Examples)”

    1. There’s no easy way to do it. Manually fetch a list of files in the folder and download them one-by-one. If there are more folders inside, a recursive function will do the trick.

Leave a Comment

Your email address will not be published. Required fields are marked *