What is the purpose of using filesize() in conjunction with fgetcsv in PHP for reading a CSV file?

When reading a CSV file using fgetcsv in PHP, it is important to check the file size before reading it to ensure that the file is not too large to be processed efficiently. By using filesize() function, we can determine the size of the file and then set a limit on how much data to read at a time to avoid memory issues.

$file = 'example.csv';
$maxFileSize = 1048576; // 1 MB

if (filesize($file) > $maxFileSize) {
    die("File size exceeds limit.");
}

$handle = fopen($file, 'r');
if ($handle !== false) {
    while (($data = fgetcsv($handle)) !== false) {
        // Process CSV data here
    }
    fclose($handle);
} else {
    die("Error opening file.");
}