evildarren Posted October 7, 2008 Share Posted October 7, 2008 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 Link to comment https://forums.phpfreaks.com/topic/127407-displaying-folder-contents-in-order-along-with-filefolder-size-and-date/ Share on other sites More sharing options...
kenrbnsn Posted October 7, 2008 Share Posted October 7, 2008 Please post the script you're using between tags. Ken Link to comment https://forums.phpfreaks.com/topic/127407-displaying-folder-contents-in-order-along-with-filefolder-size-and-date/#findComment-659033 Share on other sites More sharing options...
evildarren Posted October 7, 2008 Author Share Posted October 7, 2008 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); ?> Link to comment https://forums.phpfreaks.com/topic/127407-displaying-folder-contents-in-order-along-with-filefolder-size-and-date/#findComment-659040 Share on other sites More sharing options...
Orio Posted October 7, 2008 Share Posted October 7, 2008 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. Link to comment https://forums.phpfreaks.com/topic/127407-displaying-folder-contents-in-order-along-with-filefolder-size-and-date/#findComment-659051 Share on other sites More sharing options...
evildarren Posted October 7, 2008 Author Share Posted October 7, 2008 Cheers Orio, thats brilliant........ Link to comment https://forums.phpfreaks.com/topic/127407-displaying-folder-contents-in-order-along-with-filefolder-size-and-date/#findComment-659062 Share on other sites More sharing options...
kenrbnsn Posted October 7, 2008 Share Posted October 7, 2008 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 Link to comment https://forums.phpfreaks.com/topic/127407-displaying-folder-contents-in-order-along-with-filefolder-size-and-date/#findComment-659073 Share on other sites More sharing options...
Orio Posted October 7, 2008 Share Posted October 7, 2008 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. Link to comment https://forums.phpfreaks.com/topic/127407-displaying-folder-contents-in-order-along-with-filefolder-size-and-date/#findComment-659094 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.