I have this problem of "Undefined offset" error and a fgetcsv error. I wanted it to generate query files for me after it extracts the files from zipped file. Inside the zipped file, there are CSV files, and it will read the CSV file and generate the query and it seems like it doesn't work. I don't understand why.
Here are the errors:
Notice: Undefined offset: 5 in C:\xampp\htdocs\dnquery\process.php on line 35
Notice: Undefined offset: 5 in C:\xampp\htdocs\dnquery\process.php on line 38
Notice: Undefined offset: 5 in C:\xampp\htdocs\dnquery\process.php on line 42
Notice: Undefined offset: 5 in C:\xampp\htdocs\dnquery\process.php on line 42
Notice: Undefined offset: 5 in C:\xampp\htdocs\dnquery\process.php on line 42
Notice: Undefined offset: 5 in C:\xampp\htdocs\dnquery\process.php on line 42
Notice: Undefined offset: 5 in C:\xampp\htdocs\dnquery\process.php on line 42
Notice: Undefined offset: 8 in C:\xampp\htdocs\dnquery\process.php on line 46
Notice: Undefined offset: 8 in C:\xampp\htdocs\dnquery\process.php on line 48
Notice: Undefined variable: query in C:\xampp\htdocs\dnquery\process.php on line 53
Warning: fgetcsv(): 6 is not a valid stream resource in C:\xampp\htdocs\dnquery\process.php on line 34
Below is my code:
<?php
set_time_limit(0);
$downloadpathzip = dirname(dirname(dirname(dirname(__FILE__))))."Users\\zambo\\Downloads\\messages.zip";
$filecount = 0;
$dirdns = 'dnsquery';
if (file_exists($downloadpathzip)) {
$zip = new ZipArchive;
$res = $zip->open($downloadpathzip);
if ($res === TRUE) {
$zip->extractTo('dnsquery');
$zip->close();
unlink($downloadpathzip);
}
}
if ($handle = opendir($dirdns)) {
while (($file = readdir($handle)) !== false) {
if (!in_array($file, array('.', '..')) && !is_dir($dirdns.$file))
$filecount++;
}
}
$x = 1;
while ($x < $filecount) {
$uploadfile = "dnsquery/message_history".$x.".csv";
$filequery = 'query'.$x.'.sql';
$fpread = fopen($uploadfile,'r') or die("can't open file");
$fpwrite = fopen($filequery,'w') or die("can't open file");
while ($csv_line = fgetcsv($fpread,1024)) {
if ($csv_line[5] == "Status") {
} elseif ($csv_line[5] == "delivered") {
$querystatus = "SCS";
} elseif (
$csv_line[5] == "expired" ||
$csv_line[5] == "failed" ||
$csv_line[5] == "invalid" ||
$csv_line[5] == "outofcredit" ||
$csv_line[5] == "undeliverable"
) {
$querystatus = "FLR";
}
if ($csv_line[8] == "Reference") {
} elseif ($csv_line[8] != "" ) {
$query .= "Update smsstatus set Dnstatus = '$querystatus',updatedate=NOW() where mtmsgid = '$csv_line[8]';
";
}
fclose($fpread) or die("can't close file");
fwrite($fpwrite, $query);
fclose($fpwrite) or die("can't close file");
}
$x++;
}
?>