hi i want to delete line from my csv file after echo or process this line can any one help???????????? thanks in advance!!!!!

here is my code

$fp = fopen('surInput.csv','r') or die("can't open file");
while($csv_line = fgetcsv($fp,1024)) {
    for ($col = 0, $j = count($csv_line); $col < $j; $col++) {
        echo $csv_line[$col];
		echo "<br>";
		//now after show or processed i want to delete this line  ($csv_line)
		
    }
}
fclose($fp) or die("can't close file");

Dani AI

Generated

The CSV file can't be edited "in place" to remove an arbitrary line without rewriting the rest of the file; as said, the classic method is to write a new file and replace the old one. For very large, sequential-processing workloads (like 's 100k+ rows), a much cheaper and safer pattern is to treat the CSV as an append-only log and keep a small position file that records the byte offset of the next unprocessed record. That avoids rewriting the big file on every row while guaranteeing each record is processed exactly once.

Example (persistent byte-offset approach):

$csvFile = 'surInput.csv';
$posFile = 'surInput.pos';

$pfh = fopen($posFile, 'c+');
flock($pfh, LOCK_EX);
rewind($pfh);
$offset = (int)trim(stream_get_contents($pfh));

$fh = fopen($csvFile, 'r');
if ($offset) fseek($fh, $offset);

// read one logical CSV record (fgetcsv handles quoted / multi-line fields)
$row = fgetcsv($fh, 0);
if ($row !== false) {
    // process $row (echo, etc.)
    $newOffset = ftell($fh);
    rewind($pfh);
    ftruncate($pfh, 0);
    fwrite($pfh, (string)$newOffset);
    fflush($pfh);
}

flock($pfh, LOCK_UN);
fclose($fh);
fclose($pfh);

Notes and caveats:

  • Hold the exclusive lock on the position file while reading/updating so concurrent workers don't get the same offset.
  • Check file size on startup and reset the offset if it exceeds filesize (handles external truncation).
  • This approach does not free disk space immediately; to reclaim space, periodically compact by copying the remainder (fseek to saved offset, stream_copy_to_stream into a temp file) and atomically renaming the temp over the original.
  • If a true deletable, queryable queue is required, import the CSV into a small database (SQLite) and DELETE rows after processing — more upfront work but simplifies concurrency and space management.

Recommended Answers

All 2 Replies

There might be another way but the classic way of handling a sequential file is to write a new copy (and then replace the old copy with the new one at the end). A CSV sequential file isn't a database and it isn't meant to handle random reads and writes. As a variation on this, you could read your whole file into an array (if it isn't too big), make all of your changes then write the whole thing back to the original file (wiping out the previous contents).

my csv file is so big it has more than 1 Lac lines.
so i cant offered to delete each line in end of program,,,,i want delete each line after display or process
(display line,then delete this line=>display line,then delete this line..............)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.