Php - ftp all files in a directory?

Trying to use php to get all the files in a directory and save them locally ... It seems I constantly need to specify a name? It is right?

function grabFiles() { $conn = ftp_connect(REMOTE); @ftp_login($conn, REMOTEUSER, REMOTEPASS); ftp_get($conn, '*', FTP_BINARY); } 
+4
source share
3 answers

You can get a list of all files using ftp_nlist :
http://www.php.net/manual/en/function.ftp-nlist.php

Go through this array and upload each file using ftp_fget :
http://www.php.net/manual/en/function.ftp-fget.php

+7
source

I just wrote the ftp_glob function, since I also needed it in the project. It works exactly the same as glob () , but without any parameters.

Usage example

 $ftp = ftp_connect($server, $port, 90); ftp_login($ftp, $login, $password); ftp_pasv($ftp, true); $list = ftp_glob($ftp, "/sample/*.csv"); 

Code

 function ftp_glob($resource, $pattern) { $dir_patterns = explode('/', ltrim($pattern, '/')); if (count($dir_patterns) == 0) { return array (); } $paths_to_check = array (ftp_pwd($resource)); return ftp_glob_rec($resource, $dir_patterns, $paths_to_check); } function ftp_glob_rec($resource, $dir_patterns, $paths_to_check) { $matching_paths = array(); $pattern = array_shift($dir_patterns); foreach ($paths_to_check as $path) { $list = ftp_nlist($resource, $path); if ($list === false) { continue ; } foreach ($list as $file) { if (in_array($file, array('.', '..'))) { continue ; } if (match($file, $pattern) > 0) { $matching_paths[] = rtrim($path, '/') . '/' . $file; } } } if (count($dir_patterns) == 0) { return $matching_paths; } return ftp_glob_rec($resource, $dir_patterns, $matching_paths); } function match($string, $pattern, $a = 0, $b = 0, $options = '') { if ((!isset($string[$a])) && (!isset($pattern[$b]))) { return 1; } if (strpos($options, 'x') !== false) { if (($string[$a] == "\n") || ($string[$a] == "\t") || ($string[$a] == "\r") || ($string[$a] == " ")) { return (match($string, $pattern, ($a + 1), $b, $options)); } if (($pattern[$b] == "\n") || ($pattern[$b] == "\t") || ($pattern[$b] == "\r") || ($pattern[$b] == " ")) { return (match($string, $pattern, $a, ($b + 1), $options)); } } if ((isset($pattern[$b])) && ($pattern[$b] == '*')) { if (isset($string[$a])) { return (match($string, $pattern, ($a + 1), $b, $options) + match($string, $pattern, $a, ($b + 1), $options)); } else { return (match($string, $pattern, $a, ($b + 1), $options)); } } if ((isset($string[$a])) && (isset($pattern[$b])) && ($pattern[$b] == '?')) { return (match($string, $pattern, ($a + 1), ($b + 1), $options)); } if ((isset($string[$a])) && (isset($pattern[$b])) && ($pattern[$b] == '\\')) { if ((isset($pattern[($b + 1)])) && ($string[$a] == $pattern[($b + 1)])) { return (match($string, $pattern, ($a + 1), ($b + 2), $options)); } } if ((isset($string[$a])) && (isset($pattern[$b])) && ($string[$a] == $pattern[$b])) { return (match($string, $pattern, ($a + 1), ($b + 1), $options)); } return 0; } 
+1
source

yes, that’s right, but it would be pretty simple to use glob in a foreach loop and get all the files in a directory.

-1
source

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


All Articles