Need More Help with directories
Posted: Thu Sep 06, 2007 1:57 pm
Hi I had some help a few weeks ago on making a directory listing but now I have been trying to get the option to arrange it alphabetically by file type, or by biggest and smallest size, and so on. I have been trying for a few weeks and honestly I suck at arrays so could someone help me please? Don't have to code it all but if someone could just show me how to get the file name and the file type into one or two arrays (I'm not sure which is easier) or maybe even a string, and then have the option to sort it alphabetically by type by going index.php?sort=type or by size if it says index.php?sort=size because whenever I try to do it, it wont list the file type correctly or in alphabetical order. If I could just get an idea of what I'm supposed to do, I can make the rest of the code how I want. Thanks.
Code: Select all
<?php
function curPageURL()
{
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on")
{
$pageURL .= "s";
}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80")
{
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
}
else
{
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
print('Currently Browsing ');
echo curPageURL();
///makes a table
print('<table border="0" width="1000px" cellspacing="0" cellpadding="0"><tr><td>');
print('Name:</td>');
print('<td>');
print('Type:</td>');
print('<td>');
print('Size:</td>');
print('<td>');
print('Modified:</td></tr><tr height="20px"><td></tr></td><tr><td>');
$path = "/home/peacher/public_html/directory/";
// Open the folder
$dir_handle = @opendir($path) or die('Unable to open $path');
// Loop through the files
/////this will list directories
while ($file = readdir($dir_handle))
{
if($file == "." || $file == "index.php")////exclude files
continue;
//if the file is a folder
if((mime_content_type($file)) === 'httpd/unix-directory')
{
$folders[] = $file;////puts the folders into an array
}
//...or else it's a file
else
{
$files[] = $file;////puts the files into an array
//$filetype[] = mime_content_type($file); //puts the filetype into another array
//$filesize[] = filesize($file); //puts the size into another array
}
}
// Close
closedir($dir_handle);
sort($folders);
sort($files);
//sort($filetype);
foreach ($folders as $a) ///prints just the name
{
if($a == '..')
{
print('<a href="' . $a . '" target="_self">');
print('Parent Directory');
print('</a>');
print('</td><td>');
print('Parent Directory');// . mime_content_type($a));
print('</td><td>');
print(' - ');
print "</td><td>";
echo date("F d Y H:i:s.", fileatime($a)); ///found at
print('</tr><tr><td>');
}
else
{
print('<a href="' . $a . '" target="_self">');
echo $a;
print('</a>');
print('</td><td>');
print('Directory');// . mime_content_type($a));
print('</td><td>');
print(' - ');
print "</td><td>";
echo date("F d Y H:i:s.", fileatime($a)); ///found at http://us2.php.net/manual/en/function.fileatime.php
print('</tr><tr><td>');
}
}
foreach ($files as $a) ///prints just the name
{
print('<a href="' . $a . '" target="_self">');
echo $a;
print('</a>');
print('</td><td>');
print(' ' . mime_content_type($a));
print('</td><td>');
echo filesize($a) . ' bytes';
print "</td><td>";
echo date("F d Y H:i:s.", fileatime($a));
print('</tr><tr><td>');
}
////finishes the table
print('</td></tr></table>');
?>