chump2877
08-24-2006, 03:58 PM
I've quickly written a PHP class to get info about each file in a directory and its sub-directories....
I've got it practically working as it should, except for one small problem that I've noticed...When you sort by "file_display_name" (See the example usage), the results aren;t ordered correctly....Rather, most of the results/files are ordered ok EXCEPT for the last few results displayed....there seems to be a few out-of-order results/files displayed randomly at the end of the file list ....And I can;t seem to figure out why...it has something to do with the MultiArraySort() method, I think....
Edit: Here is an example of the problem: Click Me! (http://www.mediamogulsweb.com/client/GetDirFiles_example.php) ...I'm searching a directory called '/var/www/html/pear/' using the code from GetDirFiles_example.php in the next post...
Any help is appreciated (I appreciate anyone who is willing to spend a little time to sort through this code)...Here is the updated class file (the example usages appear in my next post since this will probably be too long for one post):
<?
// file GetDirFiles.class.php
/***********************
Class returns a multi-dimensional array containing different aspects of each file in a directory and its sub-directories. See sample usage: GetDirFiles_example.php and GetDirFiles_example2.php.
************************/
class GetDirFiles
{
var $directory;
var $base_dir;
var $file_array;
var $accepted_file_types;
var $count;
var $disable_title_tags;
var $sort_by;
var $exceptions;
var $excluded_dirs;
var $excluded_files;
function GetDirFiles($directory,$accepted_file_types,$excluded_dirs=0,$excluded_files=0,$exceptions=0,$disabl e_title_tags="false",$sort_by="title_tags")
{
$this->directory = $directory;
$this->accepted_file_types = $accepted_file_types;
$this->disable_title_tags = $disable_title_tags;
$this->sort_by = $sort_by;
$this->exceptions = $exceptions;
$this->excluded_dirs = $excluded_dirs;
$this->excluded_files = $excluded_files;
$this->base_dir = $this->directory;
}
function CreateFileArray()
{
$this->get_files();
$this->find_subdirs();
$this->file_array = $this->MultiArraySort($this->sort_by, SORT_ASC, SORT_STRING, $this->file_array);
return $this->file_array;
}
function get_files()
{
$dir_pattern = preg_replace('/\//','\/',$this->base_dir);
$handle = opendir($this->directory);
while (false !== ($file = readdir($handle)))
{
// read all files in dir
if ($file != "." && $file != "..")
{
$ext = stristr($file, '.');
if (in_array($ext,$this->accepted_file_types))
{
if ($this->excluded_files != 0)
{
if (in_array($file,$this->excluded_files))
{
continue;
}
}
if ($this->count==1)
{
$pattern1 = '/(' . $dir_pattern . ')(.*?)/';
$dir = preg_replace($pattern1,'$2',$this->directory);
if (!empty($dir))
{
$the_path = $dir.$file;
}
else
{
$the_path = $file;
}
}
else
{
$the_path = $file;
}
$path_array = explode("/",$the_path);
$file_display_name = ucfirst($path_array[count($path_array)-1]);
if ($this->disable_title_tags == "false")
{
$title_tags = $this->get_title_tags($file);
$this->file_array[] = array('file_display_name' => $file_display_name, 'the_path' => $the_path, 'title_tags' => $title_tags);
}
else
{
$this->file_array[] = array('file_display_name' => $file_display_name, 'the_path' => $the_path);
}
}
}
}
closedir($handle);
$this->count=1;
}
function find_subdirs()
{
// load subdirectories into array
$handle = opendir($this->directory);
while ($file = readdir($handle))
{
// read all files in dir
if ($file != ".." && $file != ".")
{
$abs_file = $this->directory . $file;
if (is_dir($abs_file))
{
if ($this->excluded_dirs != 0)
{
if (in_array($file,$this->excluded_dirs))
{
continue;
}
}
$subdirs[count($subdirs)] = $abs_file;
}
}
}
closedir($handle);
if (is_array($subdirs))
{
sort($subdirs);
foreach ($subdirs as $key => $val)
{
$val = str_replace("//","/",$val) . "/";
$this->directory = $val;
$this->get_files($val);
$this->find_subdirs($val);
}
}
}
function get_title_tags($file)
{
// Open file and extract title tag contents
$abs_file = $this->directory . $file;
$handle = @fopen($abs_file, "r");
if ($handle)
{
while (!feof($handle))
{
$file_content = fgets($handle, 4096);
if (preg_match('/(<title>)(.*?)(<\/title>)/i', $file_content, $matches) == 1)
{
$page_title = $matches[2];
break;
}
}
fclose($handle);
}
if (!empty($page_title))
{
if ($this->exceptions != 0)
{
foreach ($this->exceptions as $key => $val)
{
if ($this->exceptions[$key]['append_to_end'] == "no")
{
$exceptions_no[$key] = $val;
}
else
{
$exceptions_yes[$key] = $val;
}
}
if (!empty($exceptions_no))
{
foreach ($exceptions_no as $key => $val)
{
if (strpos($page_title, $exceptions_no[$key]['text']) === 0)
{
$page_title = trim(substr($page_title, $exceptions_no[$key]['char_position']));
}
}
}
if (!empty($exceptions_yes))
{
foreach ($exceptions_yes as $key => $val)
{
if (strpos($page_title, $exceptions_yes[$key]['text']) === 0)
{
$page_title = trim(substr($page_title, $exceptions_yes[$key]['char_position']));
$page_title .= ', ' . ucfirst($exceptions_yes[$key]['text']);
}
}
}
}
return $page_title;
}
else
{
return false;
}
}
function MultiArraySort()
{
$n = func_num_args();
$ar = func_get_arg($n-1);
if (!is_array($ar))
{
return false;
}
for ($i = 0; $i < $n-1; $i++)
{
$col[$i] = func_get_arg($i);
}
foreach ($ar as $key => $val)
{
foreach ($col as $kkey => $vval)
{
if (is_string($vval))
{
${"subar$kkey"}[$key] = $val[$vval];
}
}
}
$arv = array();
foreach ($col as $key => $val)
{
$arv[] = (is_string($val) ? ${"subar$key"} : $val);
}
$arv[] = $ar;
@call_user_func_array("array_multisort", $arv);
return $ar;
}
}
?>
I've got it practically working as it should, except for one small problem that I've noticed...When you sort by "file_display_name" (See the example usage), the results aren;t ordered correctly....Rather, most of the results/files are ordered ok EXCEPT for the last few results displayed....there seems to be a few out-of-order results/files displayed randomly at the end of the file list ....And I can;t seem to figure out why...it has something to do with the MultiArraySort() method, I think....
Edit: Here is an example of the problem: Click Me! (http://www.mediamogulsweb.com/client/GetDirFiles_example.php) ...I'm searching a directory called '/var/www/html/pear/' using the code from GetDirFiles_example.php in the next post...
Any help is appreciated (I appreciate anyone who is willing to spend a little time to sort through this code)...Here is the updated class file (the example usages appear in my next post since this will probably be too long for one post):
<?
// file GetDirFiles.class.php
/***********************
Class returns a multi-dimensional array containing different aspects of each file in a directory and its sub-directories. See sample usage: GetDirFiles_example.php and GetDirFiles_example2.php.
************************/
class GetDirFiles
{
var $directory;
var $base_dir;
var $file_array;
var $accepted_file_types;
var $count;
var $disable_title_tags;
var $sort_by;
var $exceptions;
var $excluded_dirs;
var $excluded_files;
function GetDirFiles($directory,$accepted_file_types,$excluded_dirs=0,$excluded_files=0,$exceptions=0,$disabl e_title_tags="false",$sort_by="title_tags")
{
$this->directory = $directory;
$this->accepted_file_types = $accepted_file_types;
$this->disable_title_tags = $disable_title_tags;
$this->sort_by = $sort_by;
$this->exceptions = $exceptions;
$this->excluded_dirs = $excluded_dirs;
$this->excluded_files = $excluded_files;
$this->base_dir = $this->directory;
}
function CreateFileArray()
{
$this->get_files();
$this->find_subdirs();
$this->file_array = $this->MultiArraySort($this->sort_by, SORT_ASC, SORT_STRING, $this->file_array);
return $this->file_array;
}
function get_files()
{
$dir_pattern = preg_replace('/\//','\/',$this->base_dir);
$handle = opendir($this->directory);
while (false !== ($file = readdir($handle)))
{
// read all files in dir
if ($file != "." && $file != "..")
{
$ext = stristr($file, '.');
if (in_array($ext,$this->accepted_file_types))
{
if ($this->excluded_files != 0)
{
if (in_array($file,$this->excluded_files))
{
continue;
}
}
if ($this->count==1)
{
$pattern1 = '/(' . $dir_pattern . ')(.*?)/';
$dir = preg_replace($pattern1,'$2',$this->directory);
if (!empty($dir))
{
$the_path = $dir.$file;
}
else
{
$the_path = $file;
}
}
else
{
$the_path = $file;
}
$path_array = explode("/",$the_path);
$file_display_name = ucfirst($path_array[count($path_array)-1]);
if ($this->disable_title_tags == "false")
{
$title_tags = $this->get_title_tags($file);
$this->file_array[] = array('file_display_name' => $file_display_name, 'the_path' => $the_path, 'title_tags' => $title_tags);
}
else
{
$this->file_array[] = array('file_display_name' => $file_display_name, 'the_path' => $the_path);
}
}
}
}
closedir($handle);
$this->count=1;
}
function find_subdirs()
{
// load subdirectories into array
$handle = opendir($this->directory);
while ($file = readdir($handle))
{
// read all files in dir
if ($file != ".." && $file != ".")
{
$abs_file = $this->directory . $file;
if (is_dir($abs_file))
{
if ($this->excluded_dirs != 0)
{
if (in_array($file,$this->excluded_dirs))
{
continue;
}
}
$subdirs[count($subdirs)] = $abs_file;
}
}
}
closedir($handle);
if (is_array($subdirs))
{
sort($subdirs);
foreach ($subdirs as $key => $val)
{
$val = str_replace("//","/",$val) . "/";
$this->directory = $val;
$this->get_files($val);
$this->find_subdirs($val);
}
}
}
function get_title_tags($file)
{
// Open file and extract title tag contents
$abs_file = $this->directory . $file;
$handle = @fopen($abs_file, "r");
if ($handle)
{
while (!feof($handle))
{
$file_content = fgets($handle, 4096);
if (preg_match('/(<title>)(.*?)(<\/title>)/i', $file_content, $matches) == 1)
{
$page_title = $matches[2];
break;
}
}
fclose($handle);
}
if (!empty($page_title))
{
if ($this->exceptions != 0)
{
foreach ($this->exceptions as $key => $val)
{
if ($this->exceptions[$key]['append_to_end'] == "no")
{
$exceptions_no[$key] = $val;
}
else
{
$exceptions_yes[$key] = $val;
}
}
if (!empty($exceptions_no))
{
foreach ($exceptions_no as $key => $val)
{
if (strpos($page_title, $exceptions_no[$key]['text']) === 0)
{
$page_title = trim(substr($page_title, $exceptions_no[$key]['char_position']));
}
}
}
if (!empty($exceptions_yes))
{
foreach ($exceptions_yes as $key => $val)
{
if (strpos($page_title, $exceptions_yes[$key]['text']) === 0)
{
$page_title = trim(substr($page_title, $exceptions_yes[$key]['char_position']));
$page_title .= ', ' . ucfirst($exceptions_yes[$key]['text']);
}
}
}
}
return $page_title;
}
else
{
return false;
}
}
function MultiArraySort()
{
$n = func_num_args();
$ar = func_get_arg($n-1);
if (!is_array($ar))
{
return false;
}
for ($i = 0; $i < $n-1; $i++)
{
$col[$i] = func_get_arg($i);
}
foreach ($ar as $key => $val)
{
foreach ($col as $kkey => $vval)
{
if (is_string($vval))
{
${"subar$kkey"}[$key] = $val[$vval];
}
}
}
$arv = array();
foreach ($col as $key => $val)
{
$arv[] = (is_string($val) ? ${"subar$key"} : $val);
}
$arv[] = $ar;
@call_user_func_array("array_multisort", $arv);
return $ar;
}
}
?>