HREF Link to open newest file, or only file in drectory?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • krobinson
    New Member
    • Dec 2006
    • 1

    HREF Link to open newest file, or only file in drectory?

    I am working on creating a link to a monthly updated pdf file. This link will be in over 100 places, so of course I don't want to update these everytime the file changes. If I can't find what I am looking for, I already use a filename.php file where I define('FILENAM E_ABC','abc.php ') and only update once - but if possible, I would like to keep this automated..... so I am hoping there is a piece of script I can use that will just pull the most recent file in a directory.

    This usually updated monthly, but there maybe times when it is not, so there maybe an issue using a java/php script where i pull the month & year into the filename. I am hoping there is a function in PHP that can do it automaticly based on the file date.

    Thank you,
    Kristine
  • michaelb
    Recognized Expert Contributor
    • Nov 2006
    • 534

    #2
    If filename includes the timestamp, you can use function scandir -
    array scandir ( string directory [, int sorting_order [, resource context]] )

    - and just pick up the first item in array (or the last item, depending on how you set parameter sorting_order)

    If not, you still can use this function to get all files in given directory, then use the filemtime function on each file... get the time and figure out the last modified file.
    int filemtime ( string filename )

    I'm not sure if this is the best, or the most efficient approach, but it should work for you.

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      There are 2 things to consider:
      1. the name of the file as visually seen by the admin in the directory, that gives some assurance over the age of the file
      2. you want a routine that returns the newest file, based on its system date.

      For no 1 you must establish some file name convention, such as: My_name YYYY-MM-DD HHMMSS.inf
      [php] // this way to construct a new file name
      $outfile = "docs/My_name ".date("Y-m-d His").".inf";[/php]
      2. a routine that, based on the above file name convention, returns the name of the newest file (last written). When you call this routine in the function that opens the file, you are sure to have the newest.[php]<?php
      $path = "docs/";
      // show the most recent file
      echo "Most recent file is: ".getNewestFN($ path);

      // Returns the name of the newest file
      // (My_name YYYY-MM-DD HHMMSS.inf)
      function getNewestFN ($path) {
      // store all .inf names in array
      $p = opendir($path);
      while (false !== ($file = readdir($p))) {
      if (strstr($file," .inf"))
      $list[]=date("YmdHis ", filemtime($path .$file)).$path. $file;
      }
      // sort array descending
      rsort($list);
      // return newest file name
      return $list[0];
      }
      ?>[/php]
      Ronald :cool:

      Comment

      Working...