Jump to content

Reading Most Recent CSV File in Directory


etocaj

Recommended Posts

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!

$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

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?

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

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.