2012-06-01 6 views
3

Nous avons un bouton lorsque nous cliquons sur ce bouton toutes les données viennent dans le fichier csv et télécharger ce fichier csv. fichier csv créer mais le code de téléchargement ne fonctionne pastélécharger le fichier csv

$fp = fopen("file\customer-list.csv", "w"); 
fileName = "file\customer-list.csv"; 
$filePath = "file\customer-list.csv"; 
$fsize = filesize("file\customer-list.csv"); 

if(($_POST['csv_download_list']) == "cm") 
{ 
    fwrite($fp, $csv); 
    fclose($fp); 
    header('Content-Description: File Transfer'); 
    header('Content-Type: application/octet-stream'); 
    header("Content-Disposition: attachment; filename=\"$fileName\""); 
    header('Content-Transfer-Encoding: binary'); 
    header('Expires: 0'); 
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
    header('Content-Length: ' . filesize($filePath)); 
    ob_clean(); 
    flush(); 
    $file = @fopen($filePath,"rb"); 
    if ($file) { 
     while(!feof($file)) { 
      print(fread($file, 1024*8)); 
      flush(); 
     } 
    @fclose($file); 
} 
exit; 
+0

pourquoi avez-vous tant de variables Lukin' autour de votre fichier. de toute façon .. je pense que ce http://stackoverflow.com/questions/1487774/generating-csv-file-and-then-forcing-the-file-to-download devrait résoudre votre problème. – Saurabh

+0

optionnellement vous pouvez également vérifier .. http://php.net/manual/fr/function.readfile.php – Saurabh

+0

S'il y a une réponse acceptable, s'il vous plaît marquer comme réponse. –

Répondre

3

Vous n'avez pas besoin d'enregistrer le fichier sur le disque, juste écho le contenu csv après avoir défini l'en-tête approprié. Essayez le code ci-dessous, il sera beaucoup plus simple

$fileName = 'customer-list.csv'; 

header('Content-Description: File Transfer'); 
header('Content-Type: application/octet-stream'); 
header('Content-Disposition: attachment; filename=' . $fileName); 
header('Content-Transfer-Encoding: binary'); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 

echo $csv; 
exit; 
16

Utilisez cet extrait devrait faire ce que vous essayez de faire.

<?php 

    $file = 'sample.csv'; //path to the file on disk 

    if (file_exists($file)) { 

     //set appropriate headers 
     header('Content-Description: File Transfer'); 
     header('Content-Type: application/csv'); 
     header('Content-Disposition: attachment; filename='.basename($file)); 
     header('Expires: 0'); 
     header('Cache-Control: must-revalidate'); 
     header('Pragma: public'); 
     header('Content-Length: ' . filesize($file)); 
     ob_clean(); 
     flush(); 

     //read the file from disk and output the content. 
     readfile($file); 
     exit; 
    } 
?> 
+1

vraiment utile, merci !!! –

0
Try... 
function download($file) { 
     if (!file_exists($file)) { 
      return false; 
     } 
     header('Content-Type: application/csv'); 
     header('Content-Disposition: attachment; filename="' . basename($file) . '"'); 
     header('Content-Length: ' . filesize($file)); 
     readfile($file); 
     exit(); 
    } 
$file = "file.csv"; 
download($file); 
Questions connexes