Uploading a folder via FTP using PHP

How can I download a folder from a remote host using FTP and PHP?

I have a username and password and a download folder ...

litter ()?

Let me know, thanks!

+6
source share
4 answers
<?php $ftp_server = "ftp.example.com"; $conn_id = ftp_connect ($ftp_server) or die("Couldn't connect to $ftp_server"); $login_result = ftp_login($conn_id, "user", "pass"); if ((!$conn_id) || (!$login_result)) die("FTP Connection Failed"); ftp_sync ("DirectoryToCopy"); // Use "." if you are in the current directory ftp_close($conn_id); // ftp_sync - Copy directory and file structure function ftp_sync ($dir) { global $conn_id; if ($dir != ".") { if (ftp_chdir($conn_id, $dir) == false) { echo ("Change Dir Failed: $dir<BR>\r\n"); return; } if (!(is_dir($dir))) mkdir($dir); chdir ($dir); } $contents = ftp_nlist($conn_id, "."); foreach ($contents as $file) { if ($file == '.' || $file == '..') continue; if (@ftp_chdir($conn_id, $file)) { ftp_chdir ($conn_id, ".."); ftp_sync ($file); } else ftp_get($conn_id, $file, $file, FTP_BINARY); } ftp_chdir ($conn_id, ".."); chdir (".."); } ?> 

Source: http://www.php.net/manual/es/function.ftp-get.php#90910

+12
source

You have a choice:

  • ftp wrapper:

     $handle = opendir('ftp://user: password@host /path/to/dir') || die(); while (false !== ($file = readdir($handle))) { if(is_file($file)){ $c = file_get_contents($file); file_put_contents('/local/'.basename($file), $c); } } closedir($handle); 
  • using php php extension

     $c = ftp_connect('host.com'); ftp_login($c, 'file', 'password'); ftp_chdir($c, '/remote/dir'); $contents = ftp_nlist($c, '-la .'); foreach($contents as $line){ $file = preg_split('@\ s+@ ', trim($line)); $name = $file[8]; $size = $file[4]; $mode = $file[0]; if(substr($mode, 0, 1) == '-'){ //file $fd = fopen('/local/path/'.$name, 'w'); ftp_fget ($c, $fd, $name, FTP_BINARY); fclose($fd); }else{ //dir } } 
  • use an external program like wget or lftp

    • wget --recursive [options] ftp: // user: password@host / # see wget --help
    • lftp -c commands.txt #, where command.txt will look something like this:

       connect user: password@ftp.host.com mget /path/to/remote /path/to/local 
+2
source

I just released two new libraries for such things in FTP and SFTP .

Recursively copying files and folders to a remote SFTP server (if local_path ends with the contents of a slash folder, otherwise the folder itself is loaded)

 Ftp::upload_dir($server, $user, $password, $local_path, $remote_path, $port = 22); 

Downloading the directory from a remote FTP server (if remote_dir ends with the contents of the slash folder, otherwise the folder itself is loaded)

 Ftp::download_dir($server, $user, $password, $remote_dir, $local_dir, $port = 22); 

This is true for the code here, but you will need the entire class for the dependencies of small utilities.

 /** * Download a directory from remote FTP server * * If remote_dir ends with a slash download folder content * otherwise download folder itself * * @param string $server * @param string $user * @param string $password * @param string $remote_dir * @param string $local_dir * @param int $port * * @return bool $downloaded * */ public static function download_dir($server, $user, $password, $remote_dir, $local_dir, $port = 21) { $downloaded = false; try { if(is_dir($local_dir) && is_writable($local_dir)) { if(false !== $cid = Ftp::login($server, $user, $password, $port)) { # If remote_dir do not ends with / if(!HString::ends_with($remote_dir, '/')) { # Create fisrt level directory on local filesystem $local_dir = rtrim($local_dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . basename($remote_dir); mkdir($local_dir); } # Remove trailing slash $local_dir = rtrim($local_dir, DIRECTORY_SEPARATOR); $downloaded = Ftp::download_all($cid, $remote_dir, $local_dir); ftp_close($cid); } } else { throw new Exception("Local directory does not exist or is not writable", 1); } } catch(Exception $e) { error_log("Ftp::download_dir : " . $e->getMessage()); } return $downloaded; } /** * Recursive function to download remote files * * @param ressource $cid * @param string $remote_dir * @param string $local_dir * * @return bool $download_all * */ private static function download_all($cid, $remote_dir, $local_dir) { $download_all = false; try { if(Ftp::is_dir($remote_dir, $cid)) { $files = ftp_nlist($cid, $remote_dir); if($files!==false) { $to_download = 0; $downloaded = 0; # do this for each file in the remote directory foreach ($files as $file) { # To prevent an infinite loop if ($file != "." && $file != "..") { $to_download++; # do the following if it is a directory if (Ftp::is_dir($file, $cid))// $remote_dir . DIRECTORY_SEPARATOR . { # Create directory on local filesystem mkdir($local_dir . DIRECTORY_SEPARATOR . basename($file)); # Recursive part if(Ftp::download_all($cid, $file, $local_dir . DIRECTORY_SEPARATOR . basename($file))) { $downloaded++; } } else { # Download files if(ftp_get($cid, $local_dir . DIRECTORY_SEPARATOR . basename($file), $file, FTP_BINARY)) { $downloaded++; } } } } # Check all files and folders have been downloaded if($to_download===$downloaded) { $download_all = true; } } } } catch(Exception $e) { error_log("Ftp::download_all : " . $e->getMessage()); } return $download_all; } 
+1
source
0
source

Source: https://habr.com/ru/post/885820/


All Articles