etocaj Posted February 19, 2010 Share Posted February 19, 2010 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 >=1 ; $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! Quote Link to comment Share on other sites More sharing options...
teamatomic Posted February 19, 2010 Share Posted February 19, 2010 $dir='/home/test/'; $files=scandir("$dir"); $array=array(); foreach($files as $file) { if ($file != '.' && $file != '..') { $ft=filemtime("$dir$file");//returns last modified timestamp $array[$file]=$ft; } } natsort($array); $array=array_flip($array); $last_file=array_pop($array); HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
etocaj Posted February 19, 2010 Author Share Posted February 19, 2010 Thanks for the help, I added in what you suggested but I'm unsure why it is not working. Here is what I did with the code you gave me: 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>"; $dir='./sqmpro/'; $files=scandir("$dir"); $array=array(); foreach($files as $file) { if ($file != '.' && $file != '..') { $ft=filemtime("$dir$file");//returns last modified timestamp $array[$file]=$ft; } } natsort($array); $array=array_flip($array); $last_file=array_pop($array); $content=file("$file"); for ($i = count($content) - 1; $i >=1 ; $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>"; ?> I basically took out this line and replaced it with your code $file = "myfile.csv" Where am I going wrong? Quote Link to comment Share on other sites More sharing options...
teamatomic Posted February 19, 2010 Share Posted February 19, 2010 probably right here $content=file("$file"); just where is $file located? If its in sqmpro then $content=file("$dir$file"); If you still have problems do a print_r on the array returned by your scandir() call, if thats ok then do a print_r on the array after natsort. just keep working your way down the line making sure the output is what you expect it to be. HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.