Skip to content
Advertisement

How to save csv file in /public folder using laravel?

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);
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement