Pages

[PHP 5.4] Forcer le téléchargement d'un fichier sujet

jeudi 27 mars 2014




J'essaye de créer un fichier csv et de l'envoyer directement au client, je reçoit bien la réponse mais le browser ne lance aucun télechargement, quelqu'un a une idée ?


Code:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
public function actionSuperexport() {
$ids = 0;
$subids = array();
if (isset($_POST['ids'])) {
$ids = $_POST['ids'];
}
if (isset($_POST['subIds'])) {
$subids = $_POST['subIds'];
}

$this->exportSubgrid(explode(',', $ids), $subids);
}

private function exportSubgrid($main, $sub) {
//Rebuild Activity Lookup Model
$model = new Activity('search');
$model->initSession();
$source = $model->searchBetween()->getData();

//Extract Export Vars
$export_main = $model->export('summary');
$export_sub = $model->export('detail');
//Build CSV file
exportHeaders("summaryDetails.csv");
$handle = fopen('php://output', 'w');
foreach ($source as $record)
if ($record->id == reset($main)) {
$this->_exportGrid($handle, $record, $sub, $export_main, $export_sub, $handle);
array_shift($main);
}
fprintf($handle, str_repeat("\n", 0));
fclose($handle);
readfile('php://output');
exit;
}

function exportHeaders($fn) {
// disable caching
$now = gmdate("D, d M Y H:i:s");
header("Expires: Tue, 01 Jan 2013 00:00:00 GMT");
header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
header("Last-Modified: {$now} GMT");
// force download
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
// disposition / encoding on response body
header("Content-Disposition: attachment;filename=\"{$fn}\"");
header("Content-Type: text/csv");
header("Content-Transfer-Encoding: binary");

print "\xEF\xBB\xBF";

}






Aucun commentaire:

Enregistrer un commentaire