<?php

//https://stackoverflow.com/questions/2205738/how-to-delete-files-from-directory-based-on-creation-date-in-php

// Deleting Files Older Than X Days

define('_PATH', dirname(__FILE__));

function f1()
{
	$files = readdir(opendir('backups')); //read directory for files

	foreach($files as $file)
	{
		//check if the file is older than 30 days
		if(filemtime($file) > (time() + 2592000))
		{ 
			unlink($file); //delete
		}
	}

}

function f2()
{

	$path = '/path/to/files/';

	if ($handle = opendir($path)) 
	{

		while (false !== ($file = readdir($handle))) 
		{ 
			$filelastmodified = filemtime($path . $file);
			//24 hours in a day * 3600 seconds per hour
			if((time() - $filelastmodified) > 24*3600)
			{
			   unlink($path . $file);
			}

		}

		closedir($handle); 
	}

}
	


/*
$files = glob('backupfolder/*.bak');
if(count($files) > 10)
  foreach($files as $file)
    if(time() - filectime($file) > 10 * 24 * 60 * 60)
      unlink($file);
	  
*/


function f3()
{
	$days = 7;
	$path = './logs/';
	$filetypes_to_delete = array("pdf");

	// Open the directory
	if ($handle = opendir($path))
	{
		// Loop through the directory
		while (false !== ($file = readdir($handle)))
		{
			// Check the file we're doing is actually a file
			if (is_file($path.$file))
			{
				$file_info = pathinfo($path.$file);
				if (isset($file_info['extension']) && in_array(strtolower($file_info['extension']), $filetypes_to_delete))
				{
					// Check if the file is older than X days old
					if (filemtime($path.$file) < ( time() - ( $days * 24 * 60 * 60 ) ) )
					{
						// Do the deletion
						unlink($path.$file);
					}
				}
			}
		}
	}
}

function f4($type_file)
{

	// 24 sauvegardes par jours * 30 = 
	 

	//$fichier = '/homez.65/analogde/www/Massage/Backup/auto_db-backup-'.$infos.'.sql';
/*
	$chemin = '/homez.65/analogde/www/WORKAREA/WEBSITE/BLITZ/backup_blitz/';
	
	chdir($chemin);
	
	/** define the directory **/
	//$dir = "images/temp/";
	
/*	
	$file_extension = "*." .$type_file;
	
	echo $chemin.$file_extension;
/*	
	/*** cycle through all files in the directory ***/
/*	foreach (glob($chemin."*.zip") as $file) 
	//foreach (glob($chemin.$file_extension) as $file) 
/*	{

		/*** if file is 24 hours (86400 seconds) old then delete it ***/
/*		if(time() - filectime($file) > 86400 * 7) //
		{
			echo $file ."<br/>"; 
			//unlink($file);
		}
	}
*/	
	
	$dir = '/homez.65/analogde/www/WORKAREA/WEBSITE/BLITZ/backup_blitz/';
	
	
//	foreach (glob($dir."*.zip") as $file) 
	foreach (glob($dir .$type_file) as $file) 
	{

		/*** if file is 24 hours (86400 seconds) old then delete it ***/
		if(time() - filectime($file) > 86400)
		{
			//unlink($file);
			echo $file ."<br/>"; 
		}
	}

}


	f4("*.zip");
	
	f4("*.csv");
	//f4("csv");
	
?>