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

No comments:

Popular Posts