What are the potential pitfalls of using file_get_contents() and fputcsv() in PHP for handling CSV files?

Using file_get_contents() to read an entire CSV file into memory can be inefficient and memory-intensive for large files. Similarly, using fputcsv() to write CSV data directly to a file can be slow and may not handle large datasets efficiently. To address these issues, it is recommended to read and write CSV files line by line using fopen(), fgetcsv(), and fputcsv() functions in PHP.

// Open the CSV file for reading
if (($handle = fopen('data.csv', 'r')) !== false) {
    // Open a new CSV file for writing
    if (($output = fopen('output.csv', 'w')) !== false) {
        // Read and write CSV data line by line
        while (($data = fgetcsv($handle)) !== false) {
            fputcsv($output, $data);
        }
        fclose($output); // Close the output file
    }
    fclose($handle); // Close the input file
}