I am reading a directory for text files and then doing mysql INSERTS
after the ELSE. I am not sure whether it is fgetcsv or
fgets? See asterisks.
The problem is the text file is a text file not csv.
It is also delimited by |'s (pipes).
It was getting the count of 1 for $res instead of 13 because it wasn't
delimiting the array using fgetcsv.
So I tried them both fgets and fgetscsv
and the code does the count and thinks $res is a
one element array and errors out on fgets().
I put 9999 in as length because I don't know what the length is?
and I got the error wrong number of parameters.
I tried it without the length parameter and it asked for the length parameter?
I need to break up the $res array.
Below is the code up to the ELSE where I do the INSERTS.
thanks,
$dir = "link/";
$hdrArray=array("accountNo","zonenm","zoneid",
"sku","supplierName","nationalDrugCode","brandDescription","genericDescription","class","qty","unit","cost","extCost");
if ($handle = opendir ($dir)) {
while (FALSE != ($file = readdir ($handle))) {
if (!is_dir($dir . $file)) {
$location_code= substr($file, strpos($file, ".txt") -7, 3);
echo $location_code."<br />";
if (($fh = fopen($dir . $file, "r+")) != FALSE) {
$firstline = TRUE;
$headersok = TRUE;
while ($headersok && (($res = fgets($fh,9999,"|")) != FALSE)) ******
{
print_r($res);
if ($firstline) {
if (count($hdrArray) != count ($res)) {
echo count($hdrArray)."<br />";
echo count($res)."<br />";
$headersok = FALSE;
}//end if
if ($headersok) {
foreach ($res as $x) {
echo $x;
echo $hdrArray;
if (!in_array($x, $hdrArray)) {
$headersok = false;
}
}//end for each
}//end if
if ($headersok) {
foreach ($hdrArray as $x) {
echo $hdrArray;
echo $x;
if (!in_array($x, $res)) {
$headersok = false;
}
}//end for each
}//end if
$firstline = FALSE;// sets boolean to false
echo "first line of $file is wrong";
}//end if firstline
thanks,