duancong6937 2014-09-10 04:22
浏览 35
已采纳

无法生成查询文件

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++;
}
?>
  • 写回答

1条回答 默认 最新

  • douti6740 2014-09-10 05:14
    关注

    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");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思