Play Piano by Ear - Prefessional Book Reviews - Find jobs - PT Cruiser - Online Schooling

PDA

View Full Version : PHP List Recent Files in Folder - I'm close


bleucube
Jul 9th 2008, 10:55 am
I would like to list the top x number of files in a particular folder. I have a script that list the most recent, but I need something that list the top X.

Here's the current script:

<?php
# Most recently updated file in a directory


# Set up

$dir = "/home/bleucube/public_html/XXXXX";
$pattern = '\.(html|php|php4|txt )$';

$newstamp = 0;
$newname = "";
$dc = opendir($dir);
while ($fn = readdir($dc)) {
# Eliminate current directory, parent directory
if (ereg('^\.{1,2}$',$fn)) continue;
# Eliminate other pages not in pattern
if (! ereg($pattern,$fn)) continue;
$timedat = filemtime("$dir/$fn");
if ($timedat > $newstamp) {
$newstamp = $timedat;
$newname = $fn;
}
}

# $timedat is the time for the latest file
# $newname is the name of the latest file

?>

<?php
function CountDir($aDir, $aRecurse)
{
$Count = 0;

$d = dir($aDir);

while ($Entry = $d->Read())
{
if (!(($Entry == "..") || ($Entry == ".")))
{
if (Is_Dir($aDir . '/' . $Entry))
{
if ($aRecurse)
{
$Count += CountDir($aDir . '/' . $Entry, $aRecurse);
}
}
else
{
$Count++;
}
}
}

return $Count;
}
?>


Any Help?

Danltn
Jul 9th 2008, 11:27 am
Sorry, I really don't understand what you want exactly.

Could you perhaps paraphrase it?

Here's a much shorter script to get all the files in a folder, and order them by modification time.

<?php

$files = glob('*.{html,php,php4,txt}', GLOB_BRACE);
usort($files, 'filemtime_compare');

function filemtime_compare($a, $b)
{
return filemtime($a) - filemtime($b);
}

$i = 0;
$show = 5;

foreach($files as $file)
{
if($i == $show) break; else ++$i;
echo $file . ' - ' . date('D, d M y H:i:s', filemtime($file)) . '<br />' . "\n";
}

tgkprog
Jul 9th 2008, 11:37 am
here you go

<?php
/**
* finds latest x files in a folder using associative array and arsort function.
* http://sel2in.com
*/
chdir(dirname(__FILE__));
$dir = "C:/aps/xamp3/htdocs/d/sel2in/prjs/php2/p4/wikiLnk/tst3/t";
$numberOfFiles = 5;
//do not need to edit below
$pattern = '\.(html|php|php4|txt|csv|jpg)$';

$newstamp = 0;
$newname = "";
$dc = opendir($dir);
unset($fils);
while ($fn = readdir($dc)) {
# Eliminate current directory, parent directory
if (ereg('^\.{1,2}$',$fn)) continue;
# Eliminate other pages not in pattern
if (! ereg($pattern,$fn)) continue;
echo "file $fn <br>\n";
$timedat = filemtime("$dir/$fn");
//$fils["a" . $timedat . "$fn"] = $fn;//keep file name and time so if 2 files have same time they dont get lost
$fils[$fn] = $timedat;//keep file name and time so if 2 files have same time they dont get lost
/*if ($timedat > $newstamp) {
$newstamp = $timedat;
$newname = $fn;
}
*/


}

arsort ($fils, SORT_NUMERIC);
//for($i = 0; $i < $numberOfFiles ; $i++)
$fils2 = array_keys($fils);
$i = 0;
foreach($fils2 as $s){
$i++;
echo "$i " . $s . "<br>\n";
if($i == $numberOfFiles )break;

}
# $timedat is the time for the latest file
# $newname is the name of the latest file



?>

tgkprog
Jul 9th 2008, 11:45 am
Danltn's solution is better - his with a easy customize for top X and to specify which folder in a variable - now sure how thats done with glob in a more direct way



<?php


$dir = "C:/aps/xamp3/htdocs/d/sel2in/prjs/php2/p4/wikiLnk/tst3/t";
chdir($dir );
$numberOfFiles = 5;


$files = glob('*.{csv,html,php,php4,txt}', GLOB_BRACE);
usort($files, 'filemtime_compare');

function filemtime_compare($a, $b)
{
return filemtime($a) - filemtime($b);
}

$i = 0;
foreach($files as $file)
{
$i++;
echo "$i ". $file . ' - ' . date('D, d M y H:i:s', filemtime($file)) . '<br />' . "\n";
if($i == $numberOfFiles )break;
}


?>

bleucube
Jul 9th 2008, 11:52 am
TgkProg....

Very close. When I run it I receive the following results:

file index.php
file index.html
file save.php
file checker.php
1 index.php
2 index.html
3 checker.php
4 save.php

Those are the correct files in the directory but its listing them twice.

Appreciate the help!

Danltn
Jul 9th 2008, 11:56 am
Just try my script ;) It's sexy and unsuperfluous (unlike that word.)

Here's a tidied version: http://danltn.com/bin/nwc.phps (edit, fixed a typo.)

Dan

P.s. You (obviously) need glob.

bleucube
Jul 9th 2008, 12:00 pm
Bingo! Thanks!!!!

Rep added to the both of you. Cheers

tgkprog
Jul 9th 2008, 1:03 pm
Those are the correct files in the directory but its listing them twice.

Well the first time when its scanning and 2nd when its sorted - so you can comment the first echo statement

XT Gohan
Aug 26th 2008, 7:03 pm
I was looking through stuff and I was wondering, would the same concept apply to a recently modified folder? I mean, my goal is to see when the last time the folder was updated.

tgkprog
Aug 27th 2008, 12:18 am
Description
array glob ( string $pattern [, int $flags ] )

The glob() function searches for all the pathnames matching pattern according to the rules used by the libc glob() function, which is similar to the rules used by common shells.
Parameters

pattern

The pattern. No tilde expansion or parameter substitution is done.
flags

Valid flags:
GLOB_MARK - Adds a slash to each item returned
GLOB_NOSORT - Return files as they appear in the directory (no sorting)
GLOB_NOCHECK - Return the search pattern if no files matching it were found
GLOB_NOESCAPE - Backslashes do not quote metacharacters
GLOB_BRACE - Expands {a,b,c} to match 'a', 'b', or 'c'
GLOB_ONLYDIR - Return only directory entries which match the pattern
GLOB_ERR - Stop on read errors (like unreadable directories), by default errors are ignored.

Return Values

Returns an array containing the matched files/directories, an empty array if no file matched or FALSE on error.

Note: On some systems it is impossible to distinguish between empty match and an error.


so yes you can use it for folders too as long as your OS exposes the modified date - and i know that windows, unix, linux, solaris do

tgkprog
Aug 27th 2008, 12:20 am
<?php

$show = 0; // Leave as 0 for all
$dir = ''; // Leave as blank for current

if($dir) chdir($dir);
$files = glob( '*.{html,php,php4,txt}', GLOB_BRACE | GLOB_ONLYDIR); //for folders / DIR
usort( $files, 'filemtime_compare' );

function filemtime_compare( $a, $b )
{
return filemtime( $b ) - filemtime( $a );
}
$i = 0;
foreach ( $files as $file )
{
++$i;
if ( $i == $show ) break;
echo $file . ' - ' . date( 'D, d M y H:i:s', filemtime($file) ) . '<br />' . "\n"; /* This is the output line */
}



with modi to Danltn's code

XT Gohan
Aug 28th 2008, 12:08 pm
I'll look into to these global codes. And thanks, very much appreciated =)

Tjcn
Dec 19th 2008, 4:26 pm
Hi,

Is there anyway you can have the results of Danltn's code shown as a url to the relevant file with the link text as the title of the file?

Thanks in advance.

Tom

Danltn
Dec 19th 2008, 4:32 pm
Wow, some serious irony I come back to the forums after several months and I see my own code.

<?php

$show = 5;

$files = glob( '*.{html,php,php4,txt}', GLOB_BRACE );
usort( $files, create_function('$a, $b', 'return filemtime( $a ) - filemtime( $b );') );

for ( $i = 0; $i < $show; ++$i )
echo '<a href="', $file = $files[$i], '">', $file, '</a> - ', date( 'D, d M y H:i:s', filemtime($file) ), '<br />', "\n";

I've touched it up, now it's sexier than ever. :)
Just 5 lines of code, bit better than what we started with eh?

Edit: So it's just the title, without extensions:
<?php

$show = 5;

$files = glob( '*.{html,php,php4,txt}', GLOB_BRACE );
usort( $files, create_function('$a, $b', 'return filemtime( $a ) - filemtime( $b );') );

for ( $i = 0; $i < $show; ++$i )
echo '<a href="', $file = $files[$i], '">', ucfirst( strtolower(substr($file, 0, strpos($file, '.'))) ), '</a> - ', date( 'D, d M y H:i:s', filemtime($file) ), '<br />', "\n";

Tjcn
Dec 22nd 2008, 5:45 am
Thanks Danltn

That's almost what I am looking for but the Link text is showing as the filename, (which isn't easy to read) is it possible to have the link text as the file title, as it would show in a browser?

also, both versions of your script are showing the most recent files in my directory as 19 Mar 08 but the most recent is today 22 Dec 08.

also what would I leave out of the code to not show the date and time information in the browser?

Thanks in advance again

Tom

Danltn
Jan 3rd 2009, 2:58 pm
<?php
$show = 5;

$files = glob( '*.{html,php,php4,txt}', GLOB_BRACE );
usort( $files, create_function('$b, $a', 'return filemtime( $a ) - filemtime( $b );') );

for ( $i = 0; $i < $show; ++$i )
echo '<a href="', $file = $files[$i], '">', $file, '</a><br />', "\n";