FTP FILE BACKUP RECURSIVELY
- Auto Zipping All files Inside Directory
- Auto Send Zipped files to FTP at once
- Auto Cleaning sent Zipped files on local storage
<?php
define("user", "username_ftp", true); //Your FTP Username
define("password", "password_ftp", true); //Your FTP Password
define("host", "host_ftp", true); //eg: ftp.drivehq.com
define("port", "21", true); //default ftp port is 21
/*** Script_By_Dimas_Lanjaka ***/
if (defined("user") && defined("password") && defined("host")){
// Get real path for our folder// Get real path fo
$rootPath = realpath(__DIR__);
//array_map('unlink', glob("$rootPath*.zip"));
// Initialize archive object
$zip = new ZipArchive();
$cdate = str_replace('.', '-', $_SERVER['HTTP_HOST']);
$zip->open($cdate . '.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
// Create recursive directory iterator
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file)
{
// Skip directories (they would be added automatically)
if (!$file->isDir())
{
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
// Add current file to archive
$zip->addFile($filePath, $relativePath);
}
}
// Zip archive will be created only after closing object
$zip->close();
function sendfile($file){
$host = host;
$port = port;
$timeout = "60";
$user = user;
$pass = password;
$dest_file = $file;
$source_file = $file;
$ftp = ftp_connect($host,$port,$timeout);
ftp_login($ftp,$user,$pass);
ftp_pasv($ftp, true);
//if (ftp_delete($ftp, $file)) {
$ret = ftp_nb_put($ftp, $dest_file, $source_file, FTP_BINARY, FTP_AUTORESUME);
//}
while (FTP_MOREDATA == $ret)
{
// display progress bar, or someting
$ret = ftp_nb_continue($ftp);
}
}
function list_zipfiles($mydirectory) {
// directory we want to scan
$dircontents = scandir($mydirectory);
// list the contents
echo '<ul>';
foreach ($dircontents as $file) {
$extension = pathinfo($file, PATHINFO_EXTENSION);
if ($extension == 'zip') {
echo "<center><li>$file Backup Successfully</li></center>";
sendfile($file);
unlink($file);
}
}
echo '</ul>';
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Backup File Recursively</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h6 class="header1">PRODUCTION</h6>
<hr class="style1">
<?php
call_user_func('list_zipfiles', "./");
}
?>
</body>
</html>
<?php /*** Mohon Jangan Hapus Credit Copyright | Please Dont Remove Copyright Credits ***/ ?>