How to check if a CSV file has reached its end in PHP

To check if a CSV file has reached its end in PHP, you can use the feof() function to determine if the end-of-file (EOF) has been reached. The feof() function returns true if the file pointer has reached the end of the file, and false otherwise.

Here’s an example code that shows how to check if a CSV file named “data.csv” has reached its end:

 

<?php
// Open the CSV file for reading
$file = fopen("data.csv", "r");

// Loop through the rows of the CSV file
while (($row = fgetcsv($file)) !== false) {
  // Process the row data
  // ...
}

// Check if the end of the file has been reached
if (feof($file)) {
  echo "End of file reached";
} else {
  echo "Error: End of file not reached";
}

// Close the file
fclose($file);
?>