Скрипт для сохранения CSV файла напрямую без промежуточного хранения на сервере.
<?php function export_csv($data) { // No point in creating the export file on the file-system. We'll stream // it straight to the browser. Much nicer. // Open the output stream $fh = fopen('php://output', 'w'); // Start output buffering (to capture stream contents) ob_start(); // CSV Header $header = array('Field 1', 'Field 2', 'Field 3', 'Field 4', 'Field 5', 'Etc...'); fputcsv($fh, $header); // CSV Data foreach ($data as $k => $v) { $line = array($data['field_1'], $data['field_2'], $data['field_3'], $data['field_4'], $data['field_5'], $data['field_etc']); fputcsv($fh, $line); } // Get the contents of the output buffer $string = ob_get_clean(); // Set the filename of the download $filename = 'my_csv_' . date('Ymd') .'-' . date('His'); // Output CSV-specific headers header('Pragma: public'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Cache-Control: private', false); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . $filename . '.csv";'); header('Content-Transfer-Encoding: binary'); // Stream the CSV data exit($string); } ?>
https://www.perpetual-beta.org/weblog/php-stream-file-direct.html