Page 1 of 1

Need More Help with directories

Posted: Thu Sep 06, 2007 1:57 pm
by tinatina
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>');

?>

Posted: Thu Sep 06, 2007 2:13 pm
by TheMoose
Instead of using readdir(), use scandir() (www.php.net/scandir), but you need PHP5 (look at their example if you have PHP4). It will return an array of everything in the given directory, including sub-directories. Once there you can use is_dir() to check if the current item is a directory, or is_file() to check if the current item is a file.

One of the comments includes a script you might find handy, it separates files from directories into 2 different arrays:

Code: Select all

<?php
$files = scandir ( $dir ) ;
foreach( $files as $pos => $file ) {
    if( is_dir( $file ) ) {
        $dirs[] = $file ;
        unset( $files[$pos] ) ;
    }
}
?>
Once you have that you can sort the items independently. If you want a basic file type sort, just make a function to return the file extension (the stuff after the very last period, ie: file.DAT), and have it sort on that (it won't necessary be the true file type, since file extensions are easily changed). Or as you have, you can just use the MIME type as your sort mechanism. With the files array, you can use filesize() to get the file's size and then sort on that as well.

Posted: Thu Sep 06, 2007 3:33 pm
by josa

Code: Select all

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 the file names but maintain index association
asort($files);

// print files
foreach ($files as $index => $name)
{
    print "Name: " . $name . "<br>";
    print "Size: " . $filesize[$index] . "<br>";
    print "Type: " . $filetype[$index] . "<br>";
}

// sort the file sizes but maintain index association
asort($filesize);

// print files
foreach ($filesize as $index => $size)
{
    print "Name: " . $files[$index] . "<br>";
    print "Size: " . $size . "<br>";
    print "Type: " . $filetype[$index] . "<br>";
}

// I leave the last one as an exercise...
/josa