Created
March 8, 2023 11:04
-
-
Save dev-4458/0d054662829dc4af4d840faf07e9fc00 to your computer and use it in GitHub Desktop.
Revisions
-
dev-4458 created this gist
Mar 8, 2023 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,64 @@ <?php //Download All files with original same name from FTP server and save to local server using PHP 8.1 error_reporting(E_ALL); $localFilePath = "/home/public_html/media/import/"; $ftp_server = "xxx"; $ftp_user_name = "xx"; $ftp_user_pass = "xxx"; $start_time = microtime(true); function download($ftp,$directory,$localFilePath) { if (ftp_chdir($ftp, $directory)) { echo "Current directory is now: " . ftp_pwd($ftp) . "\n"; } else { echo "Couldn't change directory\n"; } $fileContents = ftp_nlist($ftp, ftp_pwd($ftp)); if(!$fileContents) { die(' no directory found'); } $toReplace='/'.$directory.'/'; foreach ($fileContents as $key => $fileName) { //echo $fileName; echo PHP_EOL; $onlyFileName = str_replace($toReplace,'',$fileName); echo $onlyFileName; echo PHP_EOL; $localFile = $localFilePath.$onlyFileName; //Target only file names which names are having only `-full` in it. if (strpos($onlyFileName, '-full') !== false) { if(ftp_get($ftp, $localFile, $fileName, FTP_BINARY)){ echo "Successfully written to $localFile"; echo PHP_EOL; } else { echo "There was an issue"; echo PHP_EOL; } } } } try{ $ftp = ftp_connect($ftp_server) or die('Could not connect to FTP server.'); $login_result = ftp_login($ftp, $ftp_user_name, $ftp_user_pass); if (!$login_result) { die('Could not log in to FTP account.'); } ftp_pasv($ftp, true); //pass directory name taken from FTP server, in this case it's 'images' download($ftp,'images',$localFilePath); echo PHP_EOL; ftp_close($ftp); $execution_time = (); echo " Execution time of script = ".(microtime(true) - $start_time)." sec"; echo PHP_EOL; } catch(\Exception $e){ print_r($e->getMessage()); }