Download a file from an FTP server and save it in a local file.


/ Published in: PHP
Save to your folder(s)

I wrote this code for when I need to get files from other servers and put them on my own server. I wanted to simplify it as much as possible.


Copy this code and paste it in your HTML
  1. function ftp_download($ftp_host, $ftp_user, $ftp_pass, $remote_file_path, $local_file_path, $passive = true, $mode = FTP_BINARY) {
  2.  
  3. $conn_id = ftp_connect($ftp_host);
  4.  
  5. if ($conn_id) {
  6.  
  7. if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) {
  8.  
  9. ftp_pasv($conn_id, $passive);
  10.  
  11. if (ftp_get($conn_id, $local_file_path, $remote_file_path, $mode)) {
  12. ftp_close($conn_id);
  13. return true;
  14. } else {
  15. ftp_close($conn_id);
  16. return false;
  17. }
  18.  
  19. } else {
  20. throw new Exception("Failed to login to the FTP server.", 100)
  21. }
  22.  
  23. } else {
  24. throw new Exception("Failed to connect to the FTP server.", 99);
  25. }
  26.  
  27. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.