0

我有一个 40031 行的 csv 文件。我必须删除文件中的所有空白行。在这里,我已经为第 13 行到第 40 行编写了一些 PHP 代码,但它不起作用。问题出在哪里?

我的 PHP 代码:

<?php 
function del_blank_row($filename, $startrow, $endrow){
    $status = 0;
    //check if file exists
    if (file_exists($filename)) {
        //end execution for invalid startrow or endrow
        if ($startrow < 0 || $endrow < 0 || $startrow > 0 && $endrow > 0 && $startrow > $endrow) {
            die('Invalid startrow or endrow value');
        }
        $updatedcsv = array();
        $count = 0;
        //open file to read contents
        $fp = fopen($filename, "r");
        //get file contents in an array
        $csvcontents = fgetcsv($fp);
        //for every row
        foreach ($csvcontents as $csvcontent) {
            if (!empty($csvcontent)) {
                array_push($updatedcsv, $csvcontent);
                continue;
            }
        }
        print_r($updatedcsv);
        $fp = fopen($filename, "w");
        fputcsv($fp, $updatedcsv);
        fclose($fp);
    } else {
        die('File does not exist');
    }
}

//-----call-----//
$csvdata=del_blank_row('h.csv', 13, 48);
4

1 回答 1

0

首先以读取模式关闭文件,然后以写入模式打开!那是在fclose($fp);声明之前$fp = fopen($filename, "w");

于 2013-09-05T05:44:58.567 回答