Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Reading Most Recent CSV File in Directory

    I thought I had wrapped this project up, but found out that the program I use to FTP a csv file to my website is best used in time stamp mode.

    That is, each day a newly named csv file is uploaded with that days data.

    I currently have this script that I use to display the data on my website:

    PHP Code:

    echo "<table align='left' width='685' bordercolor='ccc' border='1'>";
    echo 
    "<tr><td><strong>Date</strong></td><td><strong>MPSAS</strong></td><td><strong>NELM</strong></td><td><strong>Temp (C)</strong></td></td>";
    $file "myfile.csv";
    $content=file("$file");
    for (
    $i count($content) - 1$i >=$i--) { 
    $row explode(","$content[$i]); 
    echo 
    "<tr><td>{$row[0]}</td><td>{$row[1]}</td><td>{$row[2]}</td><td>{$row[7]}</td></td>";}echo "</table>";
    ?> 
    To read the most recent file I know I need to list the dir contents, sort, and then select the newest file, but I need some help getting started.

    Anyone have experience with that is willing to help me out?

    Thanks!

  • #2
    New Coder
    Join Date
    Aug 2003
    Location
    Derby, UK
    Posts
    97
    Thanks
    0
    Thanked 14 Times in 14 Posts
    Check out the readdir manual page for the basic code for opening, reading and outputting directory contents:

    http://uk2.php.net/readdir

    to do the sort, load the filenames into an array instead of printing them immediately, then sort the array and output from it e.g. (untested)

    PHP Code:
    if ($handle opendir('/path/to/files')) {
        
    $arrFiles = array();
        while (
    false !== ($file readdir($handle))) {
           
    $arrFiles[] = $file;
        }
        
    closedir($handle);
    }
    sort($arrFiles);
    echo(
    $arrFiles[0]); 
    This assumes that the file name will self-sort into correct order. If not you may need to customise the sort using usort() or similar.

    HTH,

    Dai

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    238
    Thanks
    3
    Thanked 37 Times in 37 Posts
    My 5 cents (does not address the question but only the code posted): The method you are using for reading and parsing a CSV file does not take into account several rules which the CSV format implies. Also on big files (if you ever have some) file() could be quite memory consuming. I would advise to look at the Manual entry for fgetcsv() and use this function for reading/parsing a CSV file. It should be much more CSV-format complaint than the method used by you currently.

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Sort files by last modified time in descending order:
    PHP Code:
    $files glob'./files/*.csv' );

    array_multisort(
        
    array_map'filemtime'$files ),
        
    SORT_NUMERIC,
        
    SORT_DESC,
        
    $files
    ); 
    The first file in the array will be the newest, and so on down the list to the last one being the oldest.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •