2

I have a folder which contains tons of files. My goal is to take backup of files year wise according to created or modified date after taking the backup of the files I want to delete the files as well.

Actually my server is containing pdf files of size 20GB and I want to take the backup of the files but there should be folder year wise. But I don't know how to achieve this.

$dir_path = "/pdfs/";
$pdf_arr = scandir($dir_path);

foreach($pdf_arr as $file) {
    if($file ! == '.' && $file ! == '..') {
        print_r($file);
    }
}
Al Foиce ѫ
  • 4,195
  • 12
  • 39
  • 49
A J
  • 441
  • 4
  • 20
  • Have you made any start at all - being able to list the files for example - https://stackoverflow.com/questions/2922954/getting-the-names-of-all-files-in-a-directory-with-php? – Nigel Ren Oct 05 '19 at 08:37
  • I don't have any idea about how would I start the code – A J Oct 05 '19 at 08:39
  • Just pick the right backup software and don't try to invent your own. If you really insist on doing this, please read [ask], because your actual question is nonexistant! – Ulrich Eckhardt Oct 07 '19 at 09:58
  • What is wrong with your own answer? – Salman A Oct 22 '19 at 08:06

3 Answers3

5
// year and month wise backup
    public function getLastModifiedFiles() {
        $source   = public_path("pdfs");

        $destination = public_path("destination"); 

        $smallest_time=INF;

        $oldest_file='';

        if ($handle = opendir($source)) {
            while (false !== ($file = readdir($handle))) {              
                if($file !== '.' && $file !== '..') {
                    $time = filemtime($source.'/'.$file);
                    $mm   = date('m', $time);
                    $yr   = date('Y', $time);
                    \File::isDirectory($destination.'/'.$yr."/$mm") or \File::makeDirectory($destination.'/'.$yr."/$mm", 0777, true, true);
                    $moveFile="$destination/$yr/$mm/$file";
                    //dd($source.'/'.$file);
                    if(!is_dir($source.'/'.$file)) {
                        if (copy($source.'/'.$file, $moveFile)) 
                        {
                          unlink($source.'/'.$file);
                        }   
                    }                     
                }
            }
            closedir($handle);
        }    

    }
A J
  • 441
  • 4
  • 20
0

In my Opinion, this will be the best solution for your requirments:

<?php

// Constans to Define
$active_dir = "pdfs/";                // Directory where your files are stored * WITH ENDING "/"
$backup_dir = "pdfs_backup/";         // Directory where the files should be moved to backup * WITH ENDING "/"
$backup_time_str = "Y";               // "Y" will backup in yearly folder structure (ex 2019), "Y-m" will backup in monthly folder (ex 2019-01), "Y-m-d" will back up in daily folder (ex 2019-01-05)
$min_file_age = time()-(3600*24*365); // only BackUp files older than actual time minus seconds (In this Case 1 Year)


// Start BackUp
backup_files_by_time($active_dir,$backup_dir,$min_file_age,$backup_time_str);

// BackUp Function
function backup_files_by_time($active_dir,$backup_dir,$min_file_age,$backup_time_str="Y") {

    $pdf_arr = scandir($active_dir);

    foreach($pdf_arr as $file) {
        if(file_exists($active_dir.$file)) {
            $filetime = filemtime($active_dir.$file); 

            // File is supposed to be backuped
            if ($filetime<$min_file_age) {
                // Create Folder if not exists
                if (!is_dir($backup_dir.date($backup_time_str,$filetime))) {mkdir($backup_dir.date($backup_time_str,$filetime));}
                // Moving File to Backupfolder
                rename($active_dir.$file,$backup_dir.date($backup_time_str,$filetime)."/".$file);
            }       
        }
    }
}

?>
MatWer
  • 136
  • 6
0

I would suggest using Symfony Finder component which is really powerful :

$finder = new Finder();
$finder->files()
       ->in('/pdfs/')
       ->date('>= 2018-01-01');

foreach($finder as $files) {
    // whatever you want to do
}

Information here

Will
  • 899
  • 8
  • 15