Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active May 26, 2023 04:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/a41bd2943c94547db39deba81fe73816 to your computer and use it in GitHub Desktop.
Save code-boxx/a41bd2943c94547db39deba81fe73816 to your computer and use it in GitHub Desktop.
PHP download entire folder with FTP

DOWNLOAD ENTIRE FOLDER WITH PHP FTP

https://code-boxx.com/download-folder-php-ftp/

NOTES

  1. Please make sure that the FTP extension is enabled in php.iniextension=ftp.
  2. Captain Obvious. Remember to change the FTP settings to your own for the below example scripts.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<?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\r\n"
: "Error downloading " . $f["name"] . "\r\n";
}}}
} else { echo "Invalid user/password"; }
// (D) CLOSE FTP CONNECTION
ftp_close($ftp);
<?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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment