I have a .csv file that has three columns in it. I want to work through the file one line at a time and parse the lines into three variables that I then insert mysql database. Here is my current code:
<?php
include('..\inc\db.inc');
$fp_in = fopen($file_name, "r");
$n = 0;
while (!feof($fp_in)) {
$rLine = fgets($fp_in, 4096);
$i = strlen($rLine);
$j = 1;
$k = 0;
$l = 0;
$test = ",";
Do {
$temp = substr($rLine, $j, 1);
if ($temp == $test) {
if ($k == '0') {
$k = $j;
} else {
$l = $j + 1;
}
}
$j++;
}While($j != $i);
$linename = substr($rLine, 0, $k);
$m = $l - $k - 1;
$k = $k + 1;
$fsp = substr($rLine, $k, $m);
$lsp = substr($rLine, $l);
}
?>
The problem is that this doesn't work. If I just parse the first line of the file, it works fine. If I set up a loop with a counter variable it works fine. As soon as I set the while condition to '!feof($fp_in)' it is an endless loop and finally times out without doing anything.
Am I doing something wrong, or is there a better way to do this??