Count files display only 3 or less

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
curtranhome
Forum Newbie
Posts: 6
Joined: Tue Jun 01, 2010 10:17 pm
Location: Sacramento, CA

Count files display only 3 or less

Post by curtranhome »

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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Count files display only 3 or less

Post by requinix »

Unless you need it recursively, use glob to get an array of files, and shuffle if you want to choose three random files.
curtranhome
Forum Newbie
Posts: 6
Joined: Tue Jun 01, 2010 10:17 pm
Location: Sacramento, CA

Re: Count files display only 3 or less

Post by curtranhome »

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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Count files display only 3 or less

Post by requinix »

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.
curtranhome
Forum Newbie
Posts: 6
Joined: Tue Jun 01, 2010 10:17 pm
Location: Sacramento, CA

Re: Count files display only 3 or less

Post by curtranhome »

Thats would do it, but my web server is not database compatible, so file systems is all I got. :banghead: 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

Post by curtranhome »

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

Post by lunarnet76 »

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
User avatar
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

Post by Jonah Bron »

This code will get the three newest ones, instead of randomly selecting them.

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);
    }
}
Just replace $results=array_rand($list,3); in lunarnet76's code with this code.
curtranhome
Forum Newbie
Posts: 6
Joined: Tue Jun 01, 2010 10:17 pm
Location: Sacramento, CA

Re: Count files display only 3 or less

Post by curtranhome »

Hey, thanks for all the replies but none of them work for me.... is there another way? maybe the php count() function?? :?:
Post Reply