Jump to content

Displaying Folder Contents in Order, along with File/Folder size and Date


evildarren

Recommended Posts

Hello,

 

I'm very new to PHP and I'm trying to build a script which will basically do what the subject says.

 

I've got a script which will do some of this (list folders/files, size and date) but wont display in any order.

 

I've also started to read up on arrays, but so far had no luck getting a script to work. 

 

???

 

After a day of messing with code and browsing for solutions I'm getting a headache......I'd be grateful for any help.

 

Regards,

 

Darren

This is the current script that I downloaded and altered....I have another version I'm playing with at the moment trying to us an array to display the info.

 


		<?

		$path = "./";
		$dir_handle = @opendir($path) or die("Unable to open $path");

		while ($file = readdir($dir_handle)) {

		if($file == "." || $file == ".." || $file == "index.php" )

		continue;
		echo "<a href=\"$file\">$file</a>   ";
		echo "   "; filesize($file); 
		echo(date ("d-m-Y", filemtime($file)));
		echo " <br />";
		}

		closedir($dir_handle);

		?>

Try:

 

<?php

$path = "./";
$dir_handle = @opendir($path) or die("Unable to open $path");
$files = array();

while ($file = readdir($dir_handle)) {
if($file == "." || $file == ".." || $file == "index.php" )
	continue;
$files[$file] = array(filesize($file), date ("d-m-Y", filemtime($file)));
}

closedir($dir_handle);

ksort($files);

foreach($files as $filename => $data)
{
echo "<a href=\"$filename\">$filename</a>   ";
echo $data[0] ."   ". $data[1];;
echo " <br />";
}

?>

 

Orio.

Here's another solution using the glob() function:

 

<?php
$path = "./*";
$files = glob($path);

foreach($files as $filename)
{
        if (basename($filename) != 'index.php' && !is_dir($filename)) {
            echo '<a href="' . $filename . '">' . basename($filename) . "</a>   ";
            echo filesize($filename) ."   ". date("d-m-Y", filemtime($filename));
            echo " <br />";
        }
}

?>

 

Ken

Yeah basically my script goes over the input at least three times- when receiving the files, sorting them (more than O(N) in this case) and when printing them. It's better to use glob() or scandir() (PHP5+) so the sorting and the "receiving" will be done together- this way it's more efficient.

 

Orio.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.