我遇到“未定义偏移”错误和fgetcsv错误的问题。我希望它在从压缩文件中提取文件后为我生成查询文件。在压缩文件中,有CSV文件,它将读取CSV文件并生成查询,看起来它不起作用。我不明白为什么。
以下是错误:
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
以下是我的代码:
<?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]';\r\n";
}
fclose($fpread) or die("can't close file");
fwrite($fpwrite, $query);
fclose($fpwrite) or die("can't close file");
}
$x++;
}
?>
答案 0 :(得分:0)
好的,您收到与文件句柄有关的错误的原因是因为您过早地关闭了句柄(我已从此示例中删除了不相关的代码):
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");
}
}
正如您所看到的那样,您正在关闭while
循环内的句柄。由于句柄被循环使用,fgetcsv()
函数将获得一个不能使用的闭合句柄。
当你完成使用它们时,你应该修改你的代码,以便在循环外面关闭句柄。
这样的事情应该有效:
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");
}