I have another problem with flat files. I want to read flat file line by line and post it as array to have easy access to each line. My base code is:
[code=php]
<?
$fp = fopen(“flatfile.txt”, “r”);
$temp = fread ($fp, filesize (“flatfile.txt”));
fclose ($fp);
$datalines = explode (“n”, $temp);
$count = count($datalines);
for ($i=0; $i<count($datalines); $i++) {
echo $i . “: ” . $datalines[$i] . “<br />”;
}
?>
It counts all lines and read show them as:
[CODE]0: 1st line
1: 2nd line
2: 3rd line (…)
I want to use it with multiple files. I want to script search in some directory for all “.txt” files and than read each of them exactly the way I showed before. Here’s what did:
[code=php]
<?
$handle = opendir(“dir”);
while (false !== ($file = readdir($handle)))
{
//reads the dir
if ($file != “.” && $file != “..”)
{ //doesn’t show current and previous dir
$ext = substr($file, -4); //gets the file extension
if ($ext == ‘.txt’) //if it is txt
{
$fp = file(“dir/” . $file);
foreach ($fp as $line){
$data = explode(“n”, $line);
for ($i=0; $i<=count($line); $i++) {
echo $i . “: ” . $data[$i] . “<br />”;
}
}
}
}
}
closedir($handle);
?>
The output is:
[CODE]0: 1st line of 1st file
1:
0: 2nd line of 1st file
1:
0: 3rd line of 1st file
1:
0: 1st line of 2nd file
1:
0: 2nd line of 2nd file
1:
0: 3rd line of 2nd file
1: (…)
But it’s wrong, it doesn’t count lines in each file properly and show them wrong ?