Showing posts with label Remove file. Show all posts
Showing posts with label Remove file. Show all posts

Friday, October 10, 2008

Remove files using file created time using PHP

/*
Author : Evergreenphp
Date : 10-10-2008
Desc : This is will delete all the 'xml' files inside the $path_dir folder
not only that but also this code checks whether the file created before 10 mins
or else that files will not be deleted

h - hour, i - minutes, s - seconds, m - month, d - date of the month, Y - Year
*/

/* Configuration setting starts */
$path_dir = "/www/htdocs/phpdocs/missing_man/general";
$cdate = date("Y-m-d h:i:s",mktime(date("h"), date("i")-10, date("s"), date("m"), date("d"), date("Y")));
$file_type = "txt";
/* Configuration setting ends */

/* Function call */
Delete_file_withtime($path_dir, $cdate, $file_type);


/* Function Definition starts */
function Delete_file_withtime($path_dir, $cdate, $file_type){

$dh = opendir($path_dir);
while (($file = readdir($dh)) !== false) {
$ext_test = explode(".", $file);
echo "
".$date_access = date("Y-m-d h:i:s", fileatime($file));
$stcon_dt = strtotime($date_access);
$encon_dt = strtotime($cdate);

if($file != "." && $file != ".." && $ext_test[1] == $file_type) {
if($encon_dt > $stcon_dt){
//@unlink($file);
echo "success";
}
}

}
}
/* Function Definition ends */
?>

Tuesday, September 23, 2008

Remove files, folders nested, PHP

Note: Pls remove the prefix space on all the HTML tags, before compile this program.

/*
Author : evergreenphp
Website : http://evergreenphp.blogspot.com
Website Info : Lots of source code
FREEly available, in PHP, javascript, AJAX,
MySQL etc.,

Note : 1. Before using this program, pls take a backup of your file and folders
2. Give folder and file permission then only this code will work nice..

Description : This program is used to remove all the file and folder in a specified folder
*/

/* Configuration settings start */

$path_file = '/www/htdocs/evergreenphp';

/* 0 - files not deleted, 1 - files will be deleted */
$display_with_destroy_files = 1;

/* 0 - files not deleted, 1 - files will be deleted */
$display_with_destroy_folders = 1;

/* Configuration settings ends */


$file_folder_list = scan_Dir($path_file);
$Files_list_array = Organize_file_list($file_folder_list, $display_with_destroy_files);
$Folders_list_array = Organize_folder_list($file_folder_list, $display_with_destroy_folders);

echo "Current Folder : " . $path_file . "< br>" ;
echo "< br>Files Found : ".count($Files_list_array);
echo "< pre>";
print_r($Files_list_array);
echo "< /pre>";

echo "< br>Folders Found : ".count($Folders_list_array);;
echo "< pre>";
print_r($Folders_list_array);
echo "< /pre>";

/* ---------------------------------------------------------------------------- */

/* List out all files and folders in the Current Directory (Specific Directory) */
function scan_Dir($dir=".") {
$arrfiles = array();
if (is_dir($dir)) {
if ($handle = opendir($dir)) {
chdir($dir);
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_dir($file)) {
//$arrfiles[] = $dir."/".$file;
$arr = scan_Dir($file);
$arrfiles[] = $dir."/".$file;
foreach ($arr as $value) {
$arrfiles[] = $dir."/".$value;
}
} else {
$arrfiles[] = $dir."/".$file;
}
}
}
chdir("../");
}
closedir($handle);
}
return $arrfiles;
}


/* Organize the files list */
function Organize_file_list($files_folders, $delete_status=0)
{
$Files_array = array();
for ($i=0; $i< count($files_folders); $i++)
{
$check = explode(".", $files_folders[$i]);
if (count($check) > 1) {

if($delete_status == 1)
@unlink($files_folders[$i]); /* Removing the files */
else
$Files_array[] = $files_folders[$i];

}
}
return $Files_array;
}

/* Organize the folders list */
function Organize_folder_list($files_folders, $delete_status=0)
{
$Files_array = array();
for ($i=0; $i< count($files_folders); $i++)
{
$check = explode(".", $files_folders[$i]);
if (count($check) == 1){

if($delete_status == 1)
@rmdir($files_folders[$i]); /* Removing the Folder */
else
$Files_array[] = $files_folders[$i];

}
}
return $Files_array;
}
?>

Popular Posts