Page 1 of 1

Newest 5 files in directory

Posted: Mon Jun 17, 2002 3:14 am
by Dave
Hello,

I am trying to write a script that will read a list of .gif files from a directory and then display the last 5 to have been created.

Any help would be much appreciated - thanks in advance ~ Dave.

Posted: Mon Jun 17, 2002 3:33 am
by twigletmac
This might help get you started:
http://www.devnetwork.net/forums/viewtopic.php?t=617

Mac

Posted: Mon Jun 17, 2002 4:35 am
by volka
in addition, this will sort the files (and directories) by their last modification time (descending)

Code: Select all

<html><body><table><?php
function mySort($a, $b)
&#123;
	return -strcmp($a&#1111;1], $b&#1111;1]);
&#125;

$root = "./"; 
$handle = opendir($root) or die('cannot open directory'); 
$files = array();
while($file = readdir($handle)) 
&#123; 
	if ($file != "." && $file != "..")
	&#123;
		$absFile = $root.$file;
		$sta = stat($absFile);
		$files&#1111;] = array($absFile, $sta&#1111;'mtime']);
	&#125;
&#125; 
closedir($handle);
usort($files, 'mySort');
print_r($files);
?></table></body></html>

Second newest file only, third newest file etc..

Posted: Thu Jun 20, 2002 3:12 am
by Dave
Right, I have this working now, but am having trouble just returning a file of my choice - ie. returning the second newest file from the directory, and then the third through to the fifth newest file.

Again, any help would be great - thanks for reading :)

Posted: Thu Jun 20, 2002 6:21 am
by volka
the most recent modified file is stored at index [0] in the array, the second at [1] and so on. so i.e.

Code: Select all

for($i=0; $i<count($files) && $i < 5; $i++)
    doFancyOutput($files&#1111;$i]&#1111;0]); // just the name (but with path)
will process the top 5 'most recent modified files' (hehe, just watched 'high fidelity' ;) )

I must be doing it wrong somewhere

Posted: Thu Jun 20, 2002 1:52 pm
by Dave
Still having problems with this, the code I'm using is..

Code: Select all

$numberOfFiles = sizeOf($fileArr);
for($t=0;$t<5;$t++)
&#123;
    $thisFile = each($fileArr);
    $thisName = $thisFile&#1111;0];
    $thisTime = $thisFile&#1111;1];
    $thisTime = gmdate("F j, Y, g:ia", $thisTime+3600);

$file_size = filesize($thisName);

if ($file_size < '10000') &#123;     Header("Content-type: "); 
    readfile ( $dir_name . $fault );  &#125; else &#123;

        Header("Content-type: "); 
    readfile ( $dir_name . $thisName  );  &#125;

&#125;
How can I get the arrays from this - I keep getting odd results :(

Thanks for your help

Posted: Thu Jun 20, 2002 2:43 pm
by volka
what are you trying to do? header, readfile, uhhhh?
Probably you want to display those files, but 5 files each with it's own content-type is a little bit to much. And what shall the browser do with them?
You have to create a valid html-page first with img-tags for those files.
If the directory is public and relative to the script's path you may use this

Code: Select all

<html><body><table><?php 
function mySort($a, $b) 
&#123; 
   return -strcmp($a&#1111;1], $b&#1111;1]); 
&#125; 

$root = "imgdir"; 
$handle = opendir($root) or die('cannot open directory'); 
$files = array(); 
while($file = readdir($handle)) 
&#123; 
   if ($file != "." && $file != "..") 
   &#123; 
      $absFile = $root.$file; 
      $sta = stat($absFile); 
      $files&#1111;] = array($absFile, $sta&#1111;'mtime'], $sta&#1111;'size']); 
   &#125; 
&#125; 
closedir($handle); 
usort($files, 'mySort'); 
foreach($files as $file)
&#123;
	print('<tr><td><img alt="'.$file&#1111;0].'" src="');
	if ($file&#1111;2] < 10000)
		print('faulty.gif');
	else
		print($root);
	print('"></td><td>'.$file&#1111;1].'</td><td>'.$file&#1111;2].'</td></tr>');
&#125;
?></table></body></html>
(doesn't check for valid image-type at all)

btw. does anybody know how to access apache's mime-type-library from within php?

what I'm trying to do

Posted: Thu Jun 20, 2002 2:51 pm
by Dave
Basically I'm working on having 5 copies of this file, each one for the last 5 new files (in this case image files).

I have directly linked them at the moment and have the page working it out so I have the latest 5, but I don't want people to link to the image direct, which is why I'm generating it through the script so that I can also add an ip check and the error check, should the image be broken (size).

Posted: Fri Jun 21, 2002 10:20 am
by will
expanding on volka's post a little (i think this may be more what you need)....

pass recent_pics() a directory (relative to root) and an array of what images you want (this would also work if you ever want to go beyond the most recent 5 to say 10 or 15).

edit the echo in the foreach($list) loop to do whatever you want to do with the pictures (it currently just prints out the name).

Code: Select all

<?php 

recent_pics("images/",array(1,2));



function recent_pics($root,$list) &#123;
	
	$handle = opendir($root) or die('cannot open directory'); 
	$files = array(); 
	while($file = readdir($handle)) 
	&#123; 
	   if ($file != "." && $file != ".." && ereg("((jpg)|(gif))$",$file)) 
	   &#123; 
		  $absFile = $root.$file; 
		  $sta = stat($absFile); 
		  $files&#1111;] = array($absFile, $sta&#1111;'mtime'], $sta&#1111;'size']); 
	   &#125; 
	&#125; 
	closedir($handle); 
	usort($files, 'mySort'); 

	foreach($list as $item) &#123;
		echo $files&#1111;$item-1]&#1111;0].'<br>';
	&#125;
&#125;

function mySort($a, $b) 
&#123; 
   return -strcmp($a&#1111;1], $b&#1111;1]); 
&#125; 

?>

Posted: Tue Jun 25, 2002 7:54 am
by volka