Forum Moderators: coopster
I want to display a html table with teh values, but I want to format the columns nicely thus I need to store the values in variables.
Would anybody have a simple example of a php script that can read a fixed format file?
I have provided an example of the file definition below.
Just a couple more question:
I need to skip the first record as that is the header, is it possible to skip the first line?
The file name changes, todays file name is: f080105.dat, it uses the date, but the date of when the file is available might not be todays date as per the example, of f080105.dat.
Is there a way of starting with todays date and working back until a valid file name is availeble.
Field Name - Field Lenght - Field Type
VAL1 - 13 - N
VAL2 - 3 - A
VAL3 - 3 - N
VAL4 - 8,6 - N
VAL5 - 8,6 - N
VAL6 - 8,6 - N
Thank you very much.
will put all of the data into array called $data. If the files are large you might not want to do this.
array_shift($data);
will get rid of the first line of headings
you can then iterate through the array with
foreach($data as $line) {
}
within the foreach control, pick off the variables with substr commands such as
$var1=substr($line,0,12);
The last problem I have now is.
The file name changes, todays file name is: f080105.dat, it uses the date, but the date of when the file is available might not be todays date as per the example, of f080105.dat.
Is there a way of starting with todays date and working back until a valid file name is availeble.
Thank you!
$dirname='path of your directory';
$handle=@opendir('$dirname');
while ($file = @readdir($handle)) {
if($file=='.'¦¦$file=='..') continue;
$date=substr($file,1,6);
$result_array[]=$date;
}
@closedir($handle);
rsort($result_array,"SORT_NUMERIC");
$lastest_date=array_shift($result_array);
You might need to check all the various new sort options and flags. I can't remember which is which right now.
$date = time(); //unix time
$file_name = "f".date(dmy,$date).".dat";
while (!file_exists($file_name)){
$date -= 86400; // previous day
$file_name = "f".date(dmy,$date).".dat";
}
I hope this helps
[added]...just be certain that some file of that format exists, or you'll have an endless loop on your hands. Some uncontrollable server problem might cause that too. To forestall a possible endless loop, you might want to add an ¦¦ that limits the loop to some number of iterations.[/added]