1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP List Recent Files in Folder - I'm close

Discussion in 'PHP' started by bleucube, Jul 9, 2008.

  1. #1
    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?
    SEMrush
     
    bleucube, Jul 9, 2008 IP
    SEMrush
  2. Danltn

    Danltn Peon

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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:
     
    Danltn, Jul 9, 2008 IP
  3. tgkprog

    tgkprog Peon

    Messages:
    28
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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:
     
    tgkprog, Jul 9, 2008 IP
  4. tgkprog

    tgkprog Peon

    Messages:
    28
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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, Jul 9, 2008 IP
    bleucube likes this.
  5. bleucube

    bleucube Peon

    Messages:
    332
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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!
     
    bleucube, Jul 9, 2008 IP
  6. Danltn

    Danltn Peon

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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.
     
    Danltn, Jul 9, 2008 IP
    bleucube likes this.
  7. bleucube

    bleucube Peon

    Messages:
    332
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Bingo! Thanks!!!!

    Rep added to the both of you. Cheers
     
    bleucube, Jul 9, 2008 IP
  8. tgkprog

    tgkprog Peon

    Messages:
    28
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Well the first time when its scanning and 2nd when its sorted - so you can comment the first echo statement
     
    tgkprog, Jul 9, 2008 IP
  9. XT Gohan

    XT Gohan Greenhorn

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #9
    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.
     
    XT Gohan, Aug 26, 2008 IP
  10. tgkprog

    tgkprog Peon

    Messages:
    28
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    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 27, 2008 IP
  11. tgkprog

    tgkprog Peon

    Messages:
    28
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
     <?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
     
    tgkprog, Aug 27, 2008 IP
  12. XT Gohan

    XT Gohan Greenhorn

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #12
    I'll look into to these global codes. And thanks, very much appreciated =)
     
    XT Gohan, Aug 28, 2008 IP
  13. Tjcn

    Tjcn Peon

    Messages:
    14
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #13
    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
     
    Tjcn, Dec 19, 2008 IP
  14. Danltn

    Danltn Peon

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #14
    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:
     
    Danltn, Dec 19, 2008 IP
  15. Tjcn

    Tjcn Peon

    Messages:
    14
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #15
    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
     
    Tjcn, Dec 22, 2008 IP
  16. Danltn

    Danltn Peon

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #16
    <?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:
     
    Danltn, Jan 3, 2009 IP