I am using Laravel 4. When I try to export a database table to CSV file I get this error,
fopen(file.csv): failed to open stream: Permission denied
This is my code,
public function getExport(){
$table = Users::all();
$filename = "file.csv";
$handle = fopen($filename, 'w+');
fputcsv($handle, array('name', 'status'));
foreach($table as $row) {
fputcsv($handle, array($row['name'], $row['status']));
}
fclose($handle);
$headers = array(
'Content-Type' => 'text/csv',
);
return Response::download($filename, 'tweets.csv', $headers);
}
How can I fix this ?
Finally I found the problem.
The file.csv
is created inside the current working directory. Which is the root directory that don't have the write permission. To fix this permission I give write permission to the folder.