Count files display only 3 or less
Moderator: General Moderators
-
curtranhome
- Forum Newbie
- Posts: 6
- Joined: Tue Jun 01, 2010 10:17 pm
- Location: Sacramento, CA
Count files display only 3 or less
Hey, I was wondering if there was a way to get php to count the amount of files in a directory, then display only 3 of those files, in my case, the newest ones. How would one go by doing this? I have tried the count() method inside of a while loop, is that the wrong angle to take?
-
curtranhome
- Forum Newbie
- Posts: 6
- Joined: Tue Jun 01, 2010 10:17 pm
- Location: Sacramento, CA
Re: Count files display only 3 or less
ok, well thanks, but I am using glob and count to get the files and the number of files. What I am trying to do is get an admin to post a news entry, then have a php script on the index page to count 3 of the newest entries (names are the entry date) and display them in a div tag. Got any ideas?
Re: Count files display only 3 or less
Use a database.
Otherwise you'll have to get the list of all files and sort by file modification time. Lots of file system hits.
Otherwise you'll have to get the list of all files and sort by file modification time. Lots of file system hits.
-
curtranhome
- Forum Newbie
- Posts: 6
- Joined: Tue Jun 01, 2010 10:17 pm
- Location: Sacramento, CA
Re: Count files display only 3 or less
Thats would do it, but my web server is not database compatible, so file systems is all I got.
So how would I do that with filesystems?
-
curtranhome
- Forum Newbie
- Posts: 6
- Joined: Tue Jun 01, 2010 10:17 pm
- Location: Sacramento, CA
Re: Count files display only 3 or less
Is this even possible, or is it taking it a long time to look up?
-
lunarnet76
- Forum Commoner
- Posts: 67
- Joined: Sun Apr 04, 2010 2:07 pm
- Location: Edinburgh
Re: Count files display only 3 or less
raw rapid version :
<?php
$list=array();
if ($handle = opendir('/path/to/files')) {
echo "Directory handle: $handle\n";
echo "Files:\n";
/* This is the correct way to loop over the directory. */
while (false !== ($file = readdir($handle))) {
echo "$file\n";
$list[]=$file;
}
closedir($handle);
}
$results=array_rand($list,3);
?>
and you will have your 3 files in the $results
<?php
$list=array();
if ($handle = opendir('/path/to/files')) {
echo "Directory handle: $handle\n";
echo "Files:\n";
/* This is the correct way to loop over the directory. */
while (false !== ($file = readdir($handle))) {
echo "$file\n";
$list[]=$file;
}
closedir($handle);
}
$results=array_rand($list,3);
?>
and you will have your 3 files in the $results
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: Count files display only 3 or less
This code will get the three newest ones, instead of randomly selecting them.
Just replace $results=array_rand($list,3); in lunarnet76's code with this code.
Code: Select all
$top3 = array(array(null,0), array(null,0), array(null,0));
foreach ($list as $item) {
$m = filectime('path/to/file/'.$item);
if ($m < $top3[0][1]) {
$top3[2] = $top3[1];
$top3[1] = $top3[0];
$top3[0] = array($item, $m);
} elseif ($m < $top3[1][1]) {
$top3[2] = $top3[1];
$top3[1] = array($item, $m);
} elseif ($m < $top3[2][1]) {
$top3[2] = array($item, $m);
}
}-
curtranhome
- Forum Newbie
- Posts: 6
- Joined: Tue Jun 01, 2010 10:17 pm
- Location: Sacramento, CA
Re: Count files display only 3 or less
Hey, thanks for all the replies but none of them work for me.... is there another way? maybe the php count() function?? 