Initial commit
This commit is contained in:
150
php/functions/func_filesystem.php
Executable file
150
php/functions/func_filesystem.php
Executable file
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
#################################################################################
|
||||
# #
|
||||
# written by benjamin ramey #
|
||||
# bsr@rameydesign.net #
|
||||
# #
|
||||
# This file contains: #
|
||||
# Functions that perform various filesystem tasks such as removing #
|
||||
# unwanted types of files/folders from an array of files and folders #
|
||||
# in a folder #
|
||||
# #
|
||||
#################################################################################
|
||||
|
||||
############################################# FUNCTION: filesys_get_folder_tree #
|
||||
# returns: html of folder tree
|
||||
# parameters: path to root folder
|
||||
function filesys_get_folder_tree($path)
|
||||
{
|
||||
global $_DOC_ROOT,$_SCRIPT_FOLDER;
|
||||
# variable that will hold all the HTML for the folder
|
||||
$folderhtml = '';
|
||||
# get folder contents in alphabetical order
|
||||
$folder_contents = scandir($path);
|
||||
# the url is used so that people can download/look at these files through
|
||||
# the browser, a doc_root path wouldn't work, of course
|
||||
$url = str_replace($_DOC_ROOT,"$_SCRIPT_FOLDER",$path);
|
||||
|
||||
# one by one, set $element to a file or folder inside the passed folder
|
||||
foreach($folder_contents as $element)
|
||||
{
|
||||
# ignore current and parent folders
|
||||
if( ($element != '.') and ($element != '..') )
|
||||
{
|
||||
# make sure each is_dir call is not using cached information
|
||||
# from last is_dir check
|
||||
clearstatcache();
|
||||
# if $element is a folder, call this function recursively and
|
||||
# create HTML for this subfolder
|
||||
if( is_dir("$path/$element") )
|
||||
{
|
||||
$folderhtml .= "
|
||||
<div class='folder_name' id='${element}_name'
|
||||
onclick='ExpandFolder(this)'
|
||||
onmouseover='ChangeFolderNameBG(this,0)'
|
||||
onmouseout='ChangeFolderNameBG(this,1)'>
|
||||
+ $element
|
||||
</div>
|
||||
<div class='sub_folder' id='${element}_folder'>";
|
||||
$folderhtml .= filesys_get_folder_tree("$path/$element");
|
||||
$folderhtml .= "
|
||||
</div>\n\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
# get the size of this file in kilobytes
|
||||
$file_size_kb = intval(filesize($path.'/'.$element) / 1024);
|
||||
$folderhtml .= "
|
||||
+ <a href='/$url/$element'>$element</a>
|
||||
($file_size_kb Kb)<br />\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $folderhtml;
|
||||
}
|
||||
|
||||
################################################ FUNCTION: filesys_dirs_present #
|
||||
# returns: true or false
|
||||
# parameters: path to directory to inspect, pattern to match directory name
|
||||
# against
|
||||
function filesys_dirs_present($path,$pattern = "/.*/")
|
||||
{
|
||||
# make sure the path passed here is really a directory we can't
|
||||
# check anything but a directory, since only directory will contain
|
||||
# other directories, duh
|
||||
$handle = is_dir($path) ? opendir($path) : NULL ;
|
||||
if(empty($handle))
|
||||
{ return(NULL); }
|
||||
|
||||
# set through each child in this directory and see if the child
|
||||
# is directory but not the parent or current directory
|
||||
# readdir returns FALSE when no children are left to inspect
|
||||
while($child = readdir($handle) and $child !== false)
|
||||
{
|
||||
# make sure each is_dir call is not using cached information
|
||||
# from last is_dir check
|
||||
clearstatcache();
|
||||
# check if this child is directory and whether it
|
||||
# matches the user given pattern or not
|
||||
if(is_dir($path.'/'.$child)
|
||||
and ($child !== '..')
|
||||
and ($child !== '.')
|
||||
and (preg_match($pattern,$child)) )
|
||||
{
|
||||
# we can return true with the first matching folder we find
|
||||
# this is an OR operation
|
||||
return(true);
|
||||
}
|
||||
}
|
||||
|
||||
# if we get here, no matching folders were found, so none exist
|
||||
# so we return false
|
||||
return(false);
|
||||
}
|
||||
|
||||
######################################### FUNCTION: filesys_get_wanted_children #
|
||||
# returns: array of wanted dir children
|
||||
# parameters: 1) path to directory; 2) command to remove [files,folders]
|
||||
# 3) a pattern to match folder/file names against
|
||||
function filesys_get_wanted_children($path,$exclude,$pattern = "/.*/")
|
||||
{
|
||||
# the cmd can be passed as either all cap or not, so conform it
|
||||
# here to lower case
|
||||
$exclude = strtolower($exclude);
|
||||
|
||||
# fill an array full of the directory children, but only, of course if
|
||||
# the passed path is really to a directory
|
||||
$children = is_dir($path) ? scandir($path) : NULL ;
|
||||
if(empty($children))
|
||||
{ return(NULL); }
|
||||
|
||||
# remove the current and parent directories
|
||||
$children = preg_grep("/^\.{1,2}$/",$children,PREG_GREP_INVERT);
|
||||
# get only the children that match the pattern
|
||||
$children = preg_grep($pattern,$children);
|
||||
|
||||
# if files are to be removed, remove them
|
||||
if($exclude == 'files')
|
||||
{
|
||||
foreach($children as $key => $value)
|
||||
{
|
||||
if(!is_dir($path.'/'.$value))
|
||||
unset($children[$key]);
|
||||
}
|
||||
}
|
||||
# if folders are to be removed, remove them
|
||||
else if($exclude == 'folders')
|
||||
{
|
||||
foreach($children as $key => $value)
|
||||
{
|
||||
if(is_dir($path.'/'.$value))
|
||||
unset($children[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
# return the array of directory children, free of the bad little ones
|
||||
return($children);
|
||||
}
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user