A J
A J

Reputation: 441

How to collect the files in a separate folder date wise from a folder in php?

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);
    }
}

Upvotes: 2

Views: 577

Answers (3)

Will
Will

Reputation: 927

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

Upvotes: 0

MatWer
MatWer

Reputation: 136

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);
            }       
        }
    }
}

?>

Upvotes: 0

A J
A J

Reputation: 441

// 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);
        }    

    }

Upvotes: 5

Related Questions