-
-
Save rhl2401/c1186bd234c1983bcd0c86476467fdd5 to your computer and use it in GitHub Desktop.
Download a directory from an FTP Server
This file contains 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 characters
<?php | |
// ftp_sync - copy directory and file structure | |
// main function witch is called recursivly | |
function ftp_sync($dir, $conn_id) { | |
if ($dir !== '.') { | |
if (ftp_chdir($conn_id, $dir) === FALSE) { | |
echo 'Change dir failed: ' . $dir . PHP_EOL; | |
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, $conn_id); | |
} else { | |
ftp_get($conn_id, $file, $file, FTP_BINARY); | |
} | |
} | |
ftp_chdir($conn_id, '..'); | |
chdir('..'); | |
} | |
// your settings | |
$ftp_server = 'server'; | |
$user = 'user'; | |
$password = 'password'; | |
$document_root = 'backup'; | |
$sync_path = '/'; | |
$run = 0; | |
// calculate if time is in between | |
$current_time = date("G:i"); | |
$start = "6:59"; | |
$stop = "1:01"; | |
$date1 = DateTime::createFromFormat('G:i', $current_time); | |
$date2 = DateTime::createFromFormat('G:i', $start); | |
$date3 = DateTime::createFromFormat('G:i', $stop); | |
if ($date1 > $date2 && $date1 < $date3) | |
{ | |
$run = 0; | |
echo 'Time is between ' . $start . ' and ' . $stop; | |
} else { | |
$run = 1; | |
echo 'Time is not between ' . $start . ' and ' . $stop . ' Taking backup now .......'; | |
} | |
// start copying | |
if ($run == 1) { | |
echo '<pre>' . PHP_EOL; | |
echo 'start copying....' . PHP_EOL; | |
$conn_id = ftp_connect($ftp_server); | |
if ($conn_id) { | |
$login_result = ftp_login($conn_id, $user, $password); | |
if ($login_result) { | |
chdir('backup'); | |
$day = date("l d-m-Y"); | |
$time = date("G:i"); | |
mkdir($day); | |
chdir($day); | |
mkdir($time); | |
chdir($time); | |
ftp_chdir($conn_id, $document_root); | |
ftp_sync($sync_path, $conn_id); | |
ftp_close($conn_id); | |
} else { | |
echo 'login to server failed!' . PHP_EOL; | |
} | |
} else { | |
echo 'connection to server failed!'; | |
} | |
echo 'done.' . PHP_EOL; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment