Forum Moderators: coopster

Message Too Old, No Replies

Reading a fixed format file

Reading a fixed format file

         

photocroatia

5:03 am on Jan 10, 2005 (gmt 0)

10+ Year Member



I have been searching for days trying to find an example of a php script that can read a fixed format file and store the values in variables.

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.

figment88

5:26 am on Jan 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$data=file('f080105.dat');

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);

photocroatia

12:42 pm on Jan 10, 2005 (gmt 0)

10+ Year Member



figment88,
Thank you very much that did the trick, works %100.

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!

photocroatia

1:17 pm on Jan 10, 2005 (gmt 0)

10+ Year Member



Just had a thought, is it possible to read any file using a whildcard?

For example any filename startinf with "f" and ending with ".dat"

f*.dat

I am using as suggested this command

$data=file('http://www.#*$!.com/f080105.dat');

Thank you.

figment88

4:24 pm on Jan 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think you have to glob all the files in the directory than sort.

$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.

Salsa

5:01 pm on Jan 10, 2005 (gmt 0)

10+ Year Member



Something like this should work:

$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]

photocroatia

11:40 am on Jan 11, 2005 (gmt 0)

10+ Year Member



Thank you very much for all your help.