Posted By

BrunoDeBarros on 08/11/10


Tagged

download FTP


Versions (?)

Who likes this?

5 people have marked this snippet as a favorite

colingardom
shuffledex
pytheas
BrunoDeBarros
qubestream


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


 / Published in: PHP
 

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.

  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  

You need to login to post a comment.