Download Entire Folder With PHP FTP (Simple Examples)

Welcome to a tutorial and example of how to download a folder with PHP FTP. Can’t find a native FTP function to download an entire folder automatically? Well, we will have to do it manually then.

To download an entire folder with PHP FTP, simply fetch the list of files and download them one by one.

  • $ftp = ftp_connect("HOST");
  • ftp_login($ftp, "USER", "PASSWORD");
  • $list = ftp_mlsd($ftp, "/");
  • foreach ($list as $f) { if ($f["type"]=="file") { ftp_get($ftp, "DESTINATION".$f["name"], $f["name"]); }}
  • ftp_close($ftp);

That should cover the basics, but read on for the detailed examples!

ⓘ 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 FTP Download Useful Bits & 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 extension is enabled in php.iniextension=ftp.
  • Captain Obvious. Remember to change the FTP settings to your own for the below example scripts…
  • 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.

 

 

PHP FTP MASS DOWNLOAD

All right, let us now get into the examples of downloading entire folders with PHP FTP.

 

DOWNLOAD ENTIRE FOLDER

1-download-folder.php
<?php
// (A) SETTINGS - CHANGE TO YOUR OWN!
$ftphost = "ftp.site.com";
$ftpuser = "USER";
$ftppass = "PASSWORD";
$source = "/";
$destination = "D:/download/";
 
// (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)) {
  // (C1) GET FILES & FOLDERS
  $files = ftp_mlsd($ftp, $source);
  print_r($files);
 
  // (C2) DOWNLOAD FILES
  if (count($files)!=0) { foreach ($files as $f) { if ($f["type"]=="file") {
    $saveTo = $destination . $f["name"];
    echo ftp_get($ftp, $saveTo, $f["name"])
    ? "Saved to $saveTo"
    : "Error downloading " . $f["name"] ;
  }}}
} else { echo "Invalid user/password"; }
 
// (D) CLOSE FTP CONNECTION
ftp_close($ftp);

This is pretty much the full version of the introduction snippet. The basic mechanics are the same, but this one comes with settings and some extra checks. Take note though, this will only download files in the base folder of the FTP server.

 

 

DOWNLOAD EVERYTHING (INCLUDING SUBFOLDERS)

2-download-all.php
<?php
// (A) SETTINGS - CHANGE TO YOUR OWN!
$ftphost = "ftp.site.com";
$ftpuser = "USER";
$ftppass = "PASSWORD";
$source = "/";
$destination = "D:/download/";
 
// (B) CONNECT & LOGIN TO FTP SERVER
$ftp = ftp_connect($ftphost) or die("Failed to connect to $ftphost");
if (!ftp_login($ftp, $ftpuser, $ftppass)) {
  ftp_close($ftp);
  die("Invalid user/password");
}
 
// (C) DOWNLOAD EVERYTHING
function ftpGetAll ($path = "/") {
  // (C1) FTP OBJECT & DESTINATION FOLDER
  global $ftp;
  global $destination;
 
  // (C2) CREATE FOLDER ON LOCAL SERVER
  $saveTo = $path=="/" ? $destination : $destination . $path;
  if (!file_exists($saveTo)) {
    if (mkdir($saveTo)) { echo "$saveTo created\r\n"; }
    else { echo "Error creating $saveTo\r\n"; return false; }
  }
 
  // (C3) GET FILES
  $files = ftp_mlsd($ftp, $path);
  if (count($files)!=0) { foreach ($files as $f) {
    // (C4) FOLDER - RECURSIVE LOOP
    if ($f["type"]=="dir") { ftpGetAll($path . $f["name"] . "/"); }
 
    // (C5) FILE - DOWNLOAD
    else {
      echo ftp_get($ftp, $saveTo . $f["name"], $path . $f["name"], FTP_BINARY)
      ? "Saved to " . $saveTo . $f["name"] . "\r\n"
      : "Error downloading " . $path . $f["name"] . "\r\n" ;
    }
  }}
}
ftpGetAll($source);
 
// (D) CLOSE FTP CONNECTION
ftp_close($ftp);

To also download the sub-folders, we have to modify the previous script a little. Keep calm and look carefully –

  • We pretty much just moved the entire file download part into function ftpGetAll().
  • If it is a folder, we call ftpGetAll() recursively to dig into the sub-folder.

That’s all. All the rest of the mechanics are the same.

 

 

USEFUL BITS & LINKS

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

 

LINKS & REFERENCES

 

INFOGRAPHIC CHEAT SHEET

Download Entire Folder With PHP FTP (Click To Enlarge)

 

THE END

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

Leave a Comment

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