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; } ?> PHP: Any Help?
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"; } PHP:
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 ?> PHP:
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; } ?> PHP:
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!
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.
Well the first time when its scanning and 2nd when its sorted - so you can comment the first echo statement
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.
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
<?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 */ } PHP: with modi to Danltn's code
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
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"; PHP: 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"; PHP:
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
<?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"; PHP:
I found this topic very useful but i think we can improve on this code by removing the extensions that are shown of the files, for example i wish to display the latest php pages from a directory without the .php page extensions being shown in the results. Could someone possibly provide the updated code? So for example: page_name_here Currently the code renders: page_name_here.php