Jump to content

[SOLVED] displaying list of files in directory by date


hellonoko

Recommended Posts

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

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.  :P

 

I'm following this subject cuz I want to know how to sort them by date too...

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>";
   	} 
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.