How To 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 just need to enable the FTP extension 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!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes PHP FTP CURL FTP
Summary & Links The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

  • Please make sure that the FTP and cURL extensions are enabled in php.ini accordingly – extension=ftp and extension=curl.
  • Captain Obvious. Remember to change the FTP settings to your own in the example scripts below.
  • This is only tested on the Filezilla FTP server and PHP 8.
If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

 

UPLOAD & DOWNLOAD USING PHP FTP

In this first section, we will walk through how to use the “legit” PHP FTP extension to upload and download files.

 

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 die("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);

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().

 

 

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 die("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.

 

 

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 die("Failed to connect to $ftphost");
 
// (C) LOGIN & FTP YOGA
if (ftp_login($ftp, $ftpuser, $ftppass)) {
  $currentDir = ftp_pwd($ftp); // Get 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.

 

UPLOAD & DOWNLOAD USING PHP CURL

If you somehow cannot get PHP FTP to work properly – This is an alternative way of using PHP cURL to work with FTP.

 

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

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().

 

 

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.

 

 

SUMMARY & LINKS

That’s all for this project, 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

 

INFOGRAPHIC CHEAT SHEET

Upload Download Files Using FTP PHP (click to enlarge)

 

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!

2 thoughts on “How To 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 *