Last active
April 27, 2017 06:22
-
-
Save vishwapriyanatha/65fd17293a2e65438273f9ce19e5c8c3 to your computer and use it in GitHub Desktop.
create and download a csv file from php script
This file contains hidden or 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
$con = mysqli_connect("localhost","my_user","my_password","my_db"); | |
$query = "select * from user table"; | |
$result = mysqli_query($con,$query); | |
$array_key = ((array) $result[0]); | |
$headers = (object) array_keys($array_key); | |
$delimiter = ","; | |
$filename = 'process.csv'; | |
$f = fopen('php://memory', 'w'); | |
fputcsv($f, (array)$headers, $delimiter); | |
foreach ($result as $line) { | |
$phpar = (array) $line; | |
fputcsv($f, $phpar, $delimiter); | |
} | |
fseek($f, 0); | |
header('Content-Type: application/csv'); | |
header('Content-Disposition: attachment; filename="' . $filename . '";'); | |
fpassthru($f); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment