<?
class FileManager {
/**
Executes a function or a method on a folder recursively
@param $arg_folder_name the absolute path of
@param $callback an object of a class that implements the exec_on_folder_callback interface
@param $arg_bool_recursive [optional] default value = false
@param $arg_depth [optional] default value = 0 the depth of the folder often left to 0
@param $arg_postfix [optional] default value = false (if = true the directory is after its contents) used to rmdir a folder
@return boolean true on success and false on failure
*/
public function exec_on_folder ($arg_folder_name, exec_on_folder_callback $callback, $arg_bool_recursive=false, $arg_depth=0, $arg_postfix=false){
if(!$arg_folder_name) return false; // if the folder name is not valid then return false
if (!file_exists($arg_folder_name)) return false; // if the folder does not exists then return false
if (!is_dir($arg_folder_name)) return false; // if it is not a folder then return false
if ($arg_folder_name[strlen($arg_folder_name)-1] != '/') $arg_folder_name .= "/"; // end the foldername with /
$dir_folder = opendir($arg_folder_name); // open the folder
while ($fs_entry = readdir($dir_folder)){ // while folder
if (is_dir($arg_folder_name . $fs_entry)) {
if($fs_entry == "." || $fs_entry == "..") continue;
if (!$arg_postfix) call_user_func(array(&$callback, "callback"), $arg_folder_name . $fs_entry, true, $arg_depth);
if ($arg_bool_recursive) $this->exec_on_folder ($arg_folder_name . $fs_entry, $callback, $arg_bool_recursive, $arg_depth+1, $arg_postfix);
if ($arg_postfix) call_user_func(array(&$callback, "callback"), $arg_folder_name . $fs_entry, true, $arg_depth);
} else { // file
call_user_func(array(&$callback, "callback"), $arg_folder_name . $fs_entry, false, $arg_depth);
} // end if dir
} // end while
closedir($dir_folder); // close the folder
return true;
} // end function exec_on_folder
//------------------------------------------------------------------------------------------------
public function list_files_in_folder ($path, $recursive=false) {
$callback = new list_files_in_folder_callback();
$this->exec_on_folder($path, $callback, $recursive);
return $callback->files;
}
//------------------------------------------------------------------------------------------------
public function list_folders_in_folder ($path, $recursive=false) {
$callback = new list_folders_in_folder_callback();
$this->exec_on_folder($path, $callback, $recursive);
return $callback->folders;
}
//------------------------------------------------------------------------------------------------
public function delete_folder ($path) {
$callback = new delete_folder_callback();
// note that postfix param is true so that folder contents are deleted before the folder itself
$bool_return = $this->exec_on_folder($path, $callback, true, 0, true);
if ($bool_return) {
rmdir($path);
}
return $bool_return;
}
//------------------------------------------------------------------------------------------------
public function empty_folder ($path, $recursive=false) {
$callback = new empty_folder_callback();
// note that postfix param is true so that folder contents are deleted before the folder itself
return $this->exec_on_folder($path, $callback, $recursive, 0, true);
}
//------------------------------------------------------------------------------------------------
public function make_folder_writable ($path) {
$callback = new make_folder_writable_callback();
// note that postfix param is true so that folder contents are deleted before the folder itself
return $this->exec_on_folder($path, $callback, true);
}
//------------------------------------------------------------------------------------------------
public function make_folder_readonly ($path) {
$callback = new make_folder_readonly_callback();
// note that postfix param is true so that folder contents are deleted before the folder itself
return $this->exec_on_folder($path, $callback, true);
}
//------------------------------------------------------------------------------------------------
public function chmod_files_in_folder ($path, $permissions) {
$callback = new chmod_files_in_folder_callback();
$callback->permissions = $permissions;
return $this->exec_on_folder($path, $callback, true);
}
//------------------------------------------------------------------------------------------------
public function chmod_folders_in_folder ($path, $permissions) {
$callback = new chmod_folders_in_folder_callback();
$callback->permissions = $permissions;
return $this->exec_on_folder($path, $callback, true);
}
//------------------------------------------------------------------------------------------------
}
/**
Interface exec_on_callback
Any Class that handles files and folders recursively must implement this interface to be executed on the found file entries. The callback function should follow this signature.
public function callback ($path, $is_dir, $depth)
*/
interface exec_on_folder_callback {
/**
The callback function called by the exec_on_folder method
@param $path is the absolute path of the found file entry
@param $is_dir is a boolean to tell the function if this entry is a directory
@param $depth the depth of the found file entry
*/
public function callback ($path, $is_dir, $depth);
}
class list_files_in_folder_callback implements exec_on_folder_callback {
public function callback ($path, $is_dir, $depth) {
if (!$is_dir) {
$this->files[] = $path;
}
}
}
class list_folders_in_folder_callback implements exec_on_folder_callback{
public function callback ($path, $is_dir, $depth) {
if ($is_dir) {
$this->folders[] = $path;
}
}
}
class delete_folder_callback implements exec_on_folder_callback{
public function callback ($path, $is_dir, $depth) {
if ($is_dir) {
rmdir($path);
} else {
unlink($path);
}
}
}
class empty_folder_callback implements exec_on_folder_callback {
public function callback ($path, $is_dir, $depth) {
if ($is_dir) {
rmdir($path);
} else {
unlink($path);
}
}
}
class make_folder_writable_callback implements exec_on_folder_callback{
public function callback ($path, $is_dir, $depth) {
if ($is_dir) {
chmod($path, 0777);
} else {
chmod($path, 0666);
}
}
}
class make_folder_readonly_callback implements exec_on_folder_callback{
public function callback ($path, $is_dir, $depth) {
if ($is_dir) {
chmod($path, 0755);
} else {
chmod($path, 0644);
}
}
}
class chmod_files_in_folder_callback implements exec_on_folder_callback{
var $permissions;
public function callback ($path, $is_dir, $depth) {
if (!$is_dir) {
chmod($path, $this->permissions);
}
}
}
class chmod_folders_in_folder_callback implements exec_on_folder_callback{
var $permissions;
public function callback ($path, $is_dir, $depth) {
if ($is_dir) {
chmod($path, $this->permissions);
}
}
}
?>
|