无法生成查询文件


Unable to generate query files

我遇到了"未定义偏移量"错误和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++;
}
?>

好吧,您收到与文件句柄有关的错误的原因是您过早地关闭了句柄(我已经从这个例子中删除了不相关的代码):

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");
}