Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active May 26, 2023 05:25
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/2174223f604058dd02b57d108f9bb8bf to your computer and use it in GitHub Desktop.
Save code-boxx/2174223f604058dd02b57d108f9bb8bf to your computer and use it in GitHub Desktop.
PHP FTP Upload Download Files

UPLOAD & DOWNLOAD FILES WITH PHP FTP

https://code-boxx.com/upload-download-files-ftp-php/

NOTES

  1. Please make sure that the FTP and cURL extensions are enabled in php.ini accordingly – extension=ftp and extension=curl.
  2. Captain Obvious to the rescue. Remember to change the FTP settings to your own in the 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.

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);
<?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);
<?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); // 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);
<?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);
<?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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment