initial commit
This commit is contained in:
58
production/php/functions/func_file.php
Normal file
58
production/php/functions/func_file.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
################################################ FUNCTION: file_process_upload #
|
||||
function file_process_upload($input_name,$save_path,$overwrite = false,$replace_regex = "/[^A-Za-z0-9]+/")
|
||||
{
|
||||
$no_errs = true;
|
||||
$clean_names = array();
|
||||
if(!is_dir($save_path))
|
||||
{
|
||||
trigger_error('Path is not a directory.',E_USER_ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!is_array($_FILES[$input_name]['tmp_name']))
|
||||
{
|
||||
foreach($_FILES[$input_name] as $key => $value)
|
||||
$_FILES[$input_name][$key] = array($_FILES[$input_name][$key]);
|
||||
}
|
||||
|
||||
foreach($_FILES[$input_name]['tmp_name'] as $key => $value)
|
||||
{
|
||||
if(!is_uploaded_file($_FILES[$input_name]['tmp_name'][$key]))
|
||||
{
|
||||
$no_errs = false;
|
||||
trigger_error('File "'.$_FILES[$input_name]['name'][$key].'" is not an uploaded file.',E_USER_ERROR);
|
||||
}
|
||||
|
||||
$clean_name = $_FILES[$input_name]['name'][$key];
|
||||
# find the extension
|
||||
preg_match("/(\.\S+)$/",$clean_name,$matches);
|
||||
$ext = $matches[1];
|
||||
# take the extension off of the file name
|
||||
$clean_name = str_replace($ext,'',$clean_name);
|
||||
$clean_name = preg_replace($replace_regex,'_',$clean_name);
|
||||
|
||||
# move the uploaded file to the new location, unless a file with the same name
|
||||
# is already there
|
||||
if(file_exists($save_path.'/'.$clean_name.$ext) and !$overwrite)
|
||||
{
|
||||
$no_errs = false;
|
||||
trigger_error('File "'.$_FILES[$input_name]['name'][$key].'" already exists.', E_USER_ERROR);
|
||||
}
|
||||
if(move_uploaded_file($_FILES[$input_name]['tmp_name'][$key],$save_path.'/'.$clean_name.$ext))
|
||||
array_push($clean_names,$clean_name.$ext);
|
||||
else
|
||||
{
|
||||
$no_errs = false;
|
||||
trigger_error('File "'.$_FILES[$input_name]['name'][$key].'" could not be moved to permanent location.', E_USER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
if(count($clean_names) == 1)
|
||||
return($clean_names[0]);
|
||||
else
|
||||
return($clean_names);
|
||||
}
|
||||
|
||||
?>
|
||||
150
production/php/functions/func_filesystem.php
Executable file
150
production/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);
|
||||
}
|
||||
|
||||
?>
|
||||
25
production/php/functions/func_xml.php
Normal file
25
production/php/functions/func_xml.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
################################################### FUNCTION: xml_random_child #
|
||||
function xml_random_child($xml_file)
|
||||
{
|
||||
$xml = xml_get_from_file($xml_file);
|
||||
$children = $xml->children();
|
||||
$count = count($children);
|
||||
|
||||
return($children[rand(0,$count-1)]);
|
||||
}
|
||||
|
||||
################################################## FUNCTION: xml_get_from_file #
|
||||
function xml_get_from_file($file_path)
|
||||
{
|
||||
if(!file_exists($file_path))
|
||||
{
|
||||
trigger_error('XML file "'.$file_path.'" does not exist!');
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return simplexml_load_file($file_path);
|
||||
}
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user