List Directory Contents
Posted: Sun Apr 05, 2009 8:20 am
Total n00b question but I'm using the following script to list the contents of the current directory and output the result:
But instead of listing the contents of the current directory I'd like to list the contents of a sub directory labelled 'uploads'. I also still need the anchor links on each file to link to their location in the sub directory.
Can anyone suggest how I can include a variable/constant to specify the sub directory and output & link it's contents?
Thanks
Code: Select all
// Get this folder and files name.
$this_script = basename(__FILE__);
$this_folder = str_replace('/'.$this_script, '', $_SERVER['SCRIPT_NAME']);
// Declare vars used beyond this point.
$file_list = array();
$folder_list = array();
$total_size = 0;
// Open the current directory...
if ($handle = opendir('.'))
{
// ...start scanning through it.
while (false !== ($file = readdir($handle)))
{
// Make sure we don't list this folder, file or their links.
if ($file != "." && $file != ".." && $file != $this_script && $file != ".DS_Store")
{
// Get file info.
$stat = stat($file); // ... slow, but faster than using filemtime() & filesize() instead.
$info = pathinfo($file);
// Organize file info.
$item['name'] = $info['filename'];
$item['lname'] = strtolower($info['filename']);
$item['ext'] = $info['extension'];
if($info['extension'] == '') $item['ext'] = '.';
$item['bytes'] = $stat['size'];
$item['size'] = bytes_to_string($stat['size'], 2);
$item['mtime'] = $stat['mtime'];
// Add files to the file list...
if($info['extension'] != '')
{
array_push($file_list, $item);
}
// ...and folders to the folder list.
else
{
array_push($folder_list, $item);
}
// Clear stat() cache to free up memory (not really needed).
clearstatcache();
// Add this items file size to this folders total size
$total_size += $item['bytes'];
}
}Can anyone suggest how I can include a variable/constant to specify the sub directory and output & link it's contents?
Thanks