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 detailed examples!
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
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
PHP FTP MASS DOWNLOAD
All right, let us now get into the examples of downloading entire folders with PHP FTP.
EXAMPLE 1) DOWNLOAD THE ENTIRE FOLDER
<?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 exit("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.
EXAMPLE 2) DOWNLOAD EVERYTHING (INCLUDING SUBFOLDERS)
<?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 exit("Failed to connect to $ftphost");
if (!ftp_login($ftp, $ftpuser, $ftppass)) {
ftp_close($ftp);
exit("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.
EXTRAS
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
- FTP – Official PHP Manual
- FTP Upload Download With PHP – Code Boxx
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!
Cool page with some good info!