113 lines
5.1 KiB
PHP
Executable File
113 lines
5.1 KiB
PHP
Executable File
<?php
|
|
#########################################################
|
|
# #
|
|
# Various filesystem related functions #
|
|
# written by benjamin ramey #
|
|
# bsr@rameydesign.net #
|
|
# #
|
|
# best viewed with: #
|
|
# resolution = 1280x960 #
|
|
# tab = 4 spaces #
|
|
# indent = 8 spaces #
|
|
# #
|
|
# #
|
|
# DEPENDENCIES #
|
|
# 1) functions/func_errors.php #
|
|
# 2) includes/constants.php #
|
|
# #
|
|
###################################################################################################### traverses a passed folder top-down and displays contents
|
|
# returns: html of folder tree
|
|
# parameters: path to root folder
|
|
function filesys_get_folder_tree($folder_path)
|
|
{
|
|
if(kDEBUG) { print 'Entering filesys_get_folder_tree.<br />'; }
|
|
$folderhtml = ""; // variable that will hold all the HTML about the folder
|
|
$folder_contents = scandir($folder_path); // get folder contents in alphabetical order
|
|
|
|
foreach( $folder_contents as $element ) { // one by one, set $element to a file or folder inside the passed folder
|
|
if( ($element != '.') and ($element != '..') ) { // ignore current and parent folders
|
|
clearstatcache(); // make sure each is_dir call is not using cached information from last is_dir check
|
|
if( is_dir("$folder_path/$element") ) { // if $element is a folder, call this function recursively and create HTML for that folder
|
|
$folderhtml .= "<div class='folder_name' id='${element}_name'";
|
|
$folderhtml .= " onclick='ExpandFolder(this)'";
|
|
$folderhtml .= " onmouseover='ChangeFolderNameBG(this,0)'";
|
|
$folderhtml .= " onmouseout='ChangeFolderNameBG(this,1)'>+ $element</div>\n";
|
|
$folderhtml .= "<div class='sub_folder' id='${element}_folder'>";
|
|
$folderhtml .= filesys_get_folder_tree( "$folder_path/$element" );
|
|
$folderhtml .= "</div>\n\n";
|
|
}
|
|
else {
|
|
$file_size_kb = intval(filesize($folder_path.'/'.$element) / 1024); // I just want the integar value of the size in KB
|
|
$folderhtml .= "+ <a href='$folder_path/$element'>$element</a>";
|
|
$folderhtml .= " ($file_size_kb Kb)\n<br />\n";
|
|
}
|
|
}
|
|
}
|
|
|
|
if(kDEBUG) { print 'Leaving filesys_get_folder_tree.<br />'; }
|
|
return $folderhtml;
|
|
}
|
|
|
|
###################################################################################################### says whether sections are in this path
|
|
# returns: true or false
|
|
# parameters: path to directory to inspect
|
|
function filesys_sections_present($path)
|
|
{
|
|
if(kDEBUG) { print 'Entering filesys_sections_present...<br />'; }
|
|
$handle = '';
|
|
|
|
if(is_dir($path)) // make sure the path passed here is really a directory
|
|
{ $handle = opendir($path) or errors_collect('path','Cannot open this directory, try again.'); }
|
|
else {
|
|
errors_collect('path','Path is not a directory, try again.','HIGH');
|
|
if(kDEBUG) { print 'Leaving filesys_sections_present, exiting false 1.<br />'; }
|
|
return(0);
|
|
}
|
|
|
|
while($child = readdir($handle) and $child !== FALSE) {
|
|
if(is_dir($path.'/'.$child)
|
|
and ($child !== '..')
|
|
and ($child !== '.')
|
|
and (substr($child,2,1)=='_') ) // all valid section folders will have the format: xx_sectionname
|
|
{
|
|
if(kDEBUG) { print 'Leaving filesys_sections_present, exiting true.<br />'; }
|
|
return(1); // as soon as we find at least one section, return true
|
|
}
|
|
}
|
|
|
|
if(kDEBUG) { print 'Leaving filesys_sections_present, exiting false 2.<br />'; }
|
|
return(0); // if we get to this point, no sections are present, so return false
|
|
}
|
|
|
|
###################################################################################################### removes unwanted subfolders/files
|
|
# returns: array of safe dir children
|
|
# parameters: 1) array of dir children; 2) path to directory; 3) command to remove [files,folders]
|
|
function filesys_remove_unwanted_children($children,$path,$cmd)
|
|
{
|
|
if(kDEBUG) { print 'Entering filesys_remove_unwanted_children...<br />'; }
|
|
|
|
$cmd = strtolower($cmd);
|
|
|
|
if(($key = array_search('.',$children)) !== FALSE) // search for '.' current dir (always undesireable)
|
|
{ array_splice($children,$key,1); } // take out current dir
|
|
if(($key = array_search('..',$children)) !== FALSE) // search for '..' parent dir (always undesireable)
|
|
{ array_splice($children,$key,1); } // take out parent dir
|
|
|
|
if($cmd == 'files') { // remove all files from directory listing
|
|
foreach($children as $child) {
|
|
if(!is_dir($path.'/'.$child))
|
|
{ array_splice($children,array_search($child,$children),1); }
|
|
}
|
|
}
|
|
elseif($cmd == 'folders') { // remove all folders from directory listing
|
|
foreach($children as $child) {
|
|
if(is_dir($path.'/'.$child))
|
|
{ array_splice($children,array_search($child,$children),1); }
|
|
}
|
|
}
|
|
|
|
if(kDEBUG) { print 'Leaving filesys_remove_unwanted_children...<br />'; }
|
|
return($children); // return the much more friendly array of directory contents
|
|
}
|
|
|
|
?>
|