FTP Backup File Recursively PHP Script


  1. Auto Zipping All files Inside Directory
  2. Auto Send Zipped files to FTP at once
  3. Auto Cleaning sent Zipped files on local storage
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),

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

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_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>";
    echo '</ul>';
<title>Backup File Recursively</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<h6 class="header1">PRODUCTION</h6>
<hr class="style1">
call_user_func('list_zipfiles', "./");
<?php /*** Mohon Jangan Hapus Credit Copyright | Please Dont Remove Copyright Credits ***/ ?>

0 Response to "FTP Backup File Recursively PHP Script"

Post a Comment

Bila Ada posting yang kurang, atau error atau yang lainnya, silahkan tinggalkan komentar agar artikel/post di perbaiki.
Jangan Lupa Cek Box "Notify Me" agar tahu komentar kamu dibalas oleh saya.

If there are any posts that are missing, or error or anything else, please leave a comment for the article / post to be fixed.
Do not Forget Check Box "Notify Me" to know our comments replied by me.

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel