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!

Link to comment
Share on other sites

$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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.