hellonoko Posted October 1, 2008 Share Posted October 1, 2008 I am trying to create a script that displays files that have been uploaded to a directory in order from newest to oldest files. So by date modified or date created. I have found a few scripts online but haven't been able to get any of them to work properly. If anyone could show me how its done or point me to a good script it would be much appreciated. Thanks, ian Quote Link to comment https://forums.phpfreaks.com/topic/126553-solved-displaying-list-of-files-in-directory-by-date/ Share on other sites More sharing options...
trq Posted October 1, 2008 Share Posted October 1, 2008 How ablout you post your attempts, tell us whats not working and we help you fix it? Quote Link to comment https://forums.phpfreaks.com/topic/126553-solved-displaying-list-of-files-in-directory-by-date/#findComment-654450 Share on other sites More sharing options...
hellonoko Posted October 1, 2008 Author Share Posted October 1, 2008 This is as far as I have been able to get with it. $path = "REPOSITORY"; $narray=array(); $dir_handle = @opendir($path) or die("Unable to open $path"); echo "Directory Listing of $path<br/>"; $i=0; while($file = readdir($dir_handle)) { if(is_dir($file)) { continue; } else if($file != '.' && $file != '..') { //echo "<a href='$path/$file'>$file</a><br/>"; $narray[$i]=$file; $i++; } } rsort($narray); for($i=0; $i<sizeof($narray); $i++) { echo "<a href=".chr(34).$path.$narray[$i].chr(34).">".$narray[$i]."</a><br/>"; } //closing the directory closedir($dir_handle); Quote Link to comment https://forums.phpfreaks.com/topic/126553-solved-displaying-list-of-files-in-directory-by-date/#findComment-654458 Share on other sites More sharing options...
shutat Posted October 1, 2008 Share Posted October 1, 2008 How about glob and asort or arsort? $arr = array(); foreach (glob("*.*") as $filename) { $arr[$filename] = filemtime($filename); } asort($arr); or arsort($arr); foreach($arr as $key => $value) { ... } Quote Link to comment https://forums.phpfreaks.com/topic/126553-solved-displaying-list-of-files-in-directory-by-date/#findComment-654463 Share on other sites More sharing options...
hellonoko Posted October 1, 2008 Author Share Posted October 1, 2008 I tired some scripts with glob in them but they just didn't do anything. I was probably using them wrong. Could you show me a functional example? Quote Link to comment https://forums.phpfreaks.com/topic/126553-solved-displaying-list-of-files-in-directory-by-date/#findComment-654464 Share on other sites More sharing options...
Brian W Posted October 1, 2008 Share Posted October 1, 2008 Sorry, will just quote the message I made yesterday. Still wondering why my attempt #1 was skipping over some directories, or is that a mystery better off forgotten? :-\ I created this file-tree script for fun, its very interesting to do it in your the root directory. File 1 = filelist.php <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Files</title> <style type="text/css"> <!-- body li { list-style-type: square; list-style-position: outside; text-indent: 10px; } --> </style> </head> <body><div id="files"> <?php $dir = 'EPM/'; if(isset($_GET['dir'])) { $dir = $_GET['dir'] ; } ?> <h3>Files in: <?php echo $dir; ?></h3> <?php foreach (glob($dir."*", GLOB_MARK) as $filename) { if(is_dir($filename)) { $tdir = $filename; echo '<a href="?dir='.$filename.'">>'.$filename.'</a><br>'; include('filelist2.php');} else { echo '<li><a href="'.$filename.'">'.$filename.'</a></li>'; }}?> </div> </body> File 2 = filelist2.php <ul><?php foreach (glob($tdir."*", GLOB_MARK) as $filename) { if(is_dir($filename)) { $tdir = $filename; echo '<a href="?dir='.$filename.'">'.$filename.'</a><br>'; include('filelist2.php');} else { echo '<li><a href="'.$filename.'">'.$filename.'</a><br></li>'; }}?>></ul> to see the root, simply have the url look like this .../filelist.php?dir=/ Also, you can change the defult folder by changing $dir = I hit the stop button after about 30 seconds, little worried about what might happen. Quote Link to comment https://forums.phpfreaks.com/topic/126553-solved-displaying-list-of-files-in-directory-by-date/#findComment-654468 Share on other sites More sharing options...
hellonoko Posted October 1, 2008 Author Share Posted October 1, 2008 Brian. Those scripts worked but the second one showed everything in all directories. But neither do any kind of sorting by date? I know how to display a list of files I just don't know how to sort it by date. Quote Link to comment https://forums.phpfreaks.com/topic/126553-solved-displaying-list-of-files-in-directory-by-date/#findComment-654471 Share on other sites More sharing options...
Brian W Posted October 1, 2008 Share Posted October 1, 2008 Sorry, that was an example of the use of glob(), thought you wanted a working example. lol I made that as a file tree. Glad to hear it worked at least. I'm following this subject cuz I want to know how to sort them by date too... Quote Link to comment https://forums.phpfreaks.com/topic/126553-solved-displaying-list-of-files-in-directory-by-date/#findComment-654475 Share on other sites More sharing options...
shutat Posted October 1, 2008 Share Posted October 1, 2008 I tired some scripts with glob in them but they just didn't do anything. I was probably using them wrong. Could you show me a functional example? Do you get an error using glob, or did it not yeild any results? Here's a basic example I tried on my local setup. <?php $arr = array(); foreach (glob("*.*") as $filename) { $arr[$filename] = filemtime($filename); echo $filename . " was last modified on " . date('M d Y, h:i:m', filemtime($filename)) . "<br>"; } asort($arr); echo "<br>"; foreach($arr as $key => $value) { echo "$key was last modified on " . date('M d Y, h:i:m', $value) . "<br>"; } arsort($arr); echo "<br>"; foreach($arr as $key => $value) { echo "$key was last modified on " . date('M d Y, h:i:m', $value) . "<br>"; } ?> HTH Quote Link to comment https://forums.phpfreaks.com/topic/126553-solved-displaying-list-of-files-in-directory-by-date/#findComment-654476 Share on other sites More sharing options...
hellonoko Posted October 1, 2008 Author Share Posted October 1, 2008 No errors with GLOB just no results. Your script seems to work. How would I modify this to list the files by date? Quote Link to comment https://forums.phpfreaks.com/topic/126553-solved-displaying-list-of-files-in-directory-by-date/#findComment-654478 Share on other sites More sharing options...
hellonoko Posted October 1, 2008 Author Share Posted October 1, 2008 Was able to get your code to do what I want: Thanks. <?php $arr = array(); foreach (glob("*.*") as $filename) { $arr[$filename] = filemtime($filename); //echo $filename . " was last modified on " . date('M d Y, h:i:m', filemtime($filename)) . "<br>"; } /// //asort($arr); //echo "<br>"; //foreach($arr as $key => $value) //{ // echo "$key was last modified on " . date('M d Y, h:i:m', $value) . "<br>"; //} /// arsort($arr); echo "<br>"; foreach($arr as $key => $value) { echo "$key<br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/126553-solved-displaying-list-of-files-in-directory-by-date/#findComment-654492 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.