I am trying to save a csv file and download it. To download I used the following code.
$export = implode(",",$header)."n"; foreach($data as $key=>$val) { $export .= implode(",",$val)."n"; } header('Content-Type: application/csv'); header('Content-Disposition: attachment; filename='.$filename); echo $export; exit();
The variables $header, $data and $filename can be different based on the program. However, this only takes care of the download portion. I want to save the csv file before downloading it. How can I do that?
Advertisement
Answer
I didn’t find any laravel specific solution. But I solved it using raw PHP.
$file = fopen($filename.'.csv','w'); //by default it uses public folder fwrite($file,$export); fclose($file);