Web Analytics Made Easy -
StatCounter List files in a directory - CodingForum

Announcement

Collapse
No announcement yet.

List files in a directory

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • List files in a directory

    Hi, I was wondering if there was a way I could list the last 3 files in a directory. For example, if I have 10 files in a directory:

    File01.html
    File02.html
    File03.html
    File04.html
    File05.html
    File06.html
    File07.html
    File08.html
    File09.html
    File10.html

    I'd like to be able to output the name of the last 3 files (File10.html, File09.html and File08.html), so that I can make a small script that shows the last files added to a certain directory (so that if I add another file, File11.html, it automatically shows File11.html, File10.html and File09.html).

    Thanks in advance

  • #2
    not sure , glob() or readir() will get the directory data for you but then you have to sort it somehow ... but if the files were ordered by date then the below may work ..

    PHP Code:
    <?php
    $yaks 
    array_reverseglob"./*.*" GLOB_NOSORT ) );
    echo 
    $yaks[0].'<br />';
    echo 
    $yaks[1].'<br />';
    echo 
    $yaks[2].'<br />';
    ?>
    or if on unix (or with cygwin/etc)
    <?php
    exec
    'ls -t ./' $yaks );
    echo 
    $yaks[0].'<br />';
    echo 
    $yaks[1].'<br />';
    echo 
    $yaks[2].'<br />';
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

    Comment


    • #3
      I'm getting the following error with the first bit of code:
      Code:
      Fatal error: Call to undefined function: glob() in c:\ibserver\www\wa-r\test.php on line 10
      Maybe it's because I installed IBServer which comes with an older version of PHP? If that's the problem, how can I update it?

      *Edit* The PHP installed on my computer es version 4.2.3.

      Comment

      Working...
      X