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++;
}
?>
Okay, the reason you are getting errors pertaining to the file handles is because you are closing the handles prematurely (I have removed irrelevant code from this example):
while ($x < $filecount) {
// Open the read/write handlers
$fpread = fopen($uploadfile,'r') or die("can't open file");
$fpwrite = fopen($filequery,'w') or die("can't open file");
// Use them...
while ($csv_line = fgetcsv($fpread,1024)) {
// Close the handles... but you are not done using them yet...
// On the next iteration of the loop you will get errors because
// you closed the handles here, but they should not be.
fclose($fpread) or die("can't close file");
fclose($fpwrite) or die("can't close file");
}
}
As you can see you are closing the handles INSIDE the while
loop. Since the handles are being used by the loop, the fgetcsv()
function will get a closed handle, which it cannot use.
You should modify your code so that the handles are closed outside the loop, when you are done using them.
Something like this should work:
while ($x < $filecount) {
// Open the read/write handlers
$fpread = fopen($uploadfile,'r') or die("can't open file");
$fpwrite = fopen($filequery,'w') or die("can't open file");
// Use them...
while ($csv_line = fgetcsv($fpread,1024)) {
// Do stuff...
}
// Close them when you are done using them...
fclose($fpread) or die("can't close file");
fclose($fpwrite) or die("can't close file");
}