Created
December 6, 2015 16:16
-
-
Save wesleysaraujo/ac84b2edf596d1391c3d to your computer and use it in GitHub Desktop.
Exporta um array resultando de uma interação SQL em CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Controller_Newsletter extends Controller{ | |
public function action_export_csv() | |
{ | |
//Return list of members Newsletter (FUELPHP) | |
$newsletters = Model_Newsletter::find('all'); | |
// Array of list array Newsletters | |
$listNewsletters = array(array('Nome', 'E-mail')); | |
foreach ($newsletters as $newsletter) { | |
array_push($listNewsletters, array( | |
'name' => $newsletter->name, | |
'email' => $newsletter->email | |
)); | |
} | |
$filePath = DOCROOT.'files/csv/NewsletterMembers.csv'; | |
if(file_exists($filePath)){ | |
unlink($filePath); | |
} | |
$fp = fopen($filePath, 'w'); | |
foreach($listNewsletters as $list){ | |
fputcsv($fp, $list); | |
} | |
$fileName = basename($filePath); | |
$fileSize = filesize($filePath); | |
header("Cache-Control: private"); | |
header("Content-Type: application/stream"); | |
header("Content-Length: ".$fileSize); | |
header("Content-Disposition: attachment; filename=".$fileName); | |
// Output file. | |
readfile ($filePath); | |
exit(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment