Quantcast
Channel: Active questions tagged header - Stack Overflow
Viewing all articles
Browse latest Browse all 699

Download a csv file using PHP

$
0
0

[Solution found] Thank you all for your replies. I changed the button to a link and it is now working as it should.

I know this subject has been discussed over and over, I've been reading possible solutions the whole day but none solves my problem.I need to make a script to create a csv from mysql data and download the file to the default downloads browser's folder.As I prior said I have tried every possible solution found.I don't have issues in getting the data and putting it in a csv. I will post a simple example from one solution I found here.

<?php    header("Content-Type: text/csv");    header("Content-Disposition: attachment; filename=file.csv");    function outputCSV($data) {      $output = fopen("php://output", "w");      foreach ($data as $row)        fputcsv($output, $row);      fclose($output);    }    outputCSV(array(      array("name 1", "age 1", "city 1"),      array("name 2", "age 2", "city 2"),      array("name 3", "age 3", "city 3")    ));?>

If I put the filename in fopen, like fopen("filename.csv","w") it saves the file in the server. I need to download the file. I believe the problem is in the header. What should I do? Any help is welcomed.

I am using latest versions of xampp, php and trying it on Chrome.

Thank you.


Viewing all articles
Browse latest Browse all 699

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>