php - Currently my code exports data to a CSV file, and stores it on the server. But I want it to download the file. How do I do this? -
i tried doing headers in past, either downloaded empty csv file or php file itself.
<?php $database = new pdo('mysql:host=localhost;dbname=db_name', "root", ""); $sql = "select card_id, card_uid, card_type cards"; $stmt = $database->prepare($sql); $stmt->execute(); $filename = 'mycsvfile-'.date('y-m-d').'.csv'; $data = fopen($filename, 'w'); while ($row = $stmt->fetch(pdo::fetch_assoc)) { fputcsv($data, $row); } fclose($data); ?>
try can result.
<?php $database = new pdo('mysql:host=localhost;dbname=db_name', "root", ""); header('content-type: text/csv; charset=utf-8'); header('content-disposition: attachment; filename=data.csv'); $sql = "select card_id, card_uid, card_type cards"; $stmt = $database->prepare($sql); $stmt->execute(); $data = fopen('php://output', 'w'); while ($row = $stmt->fetch(pdo::fetch_assoc)) { fputcsv($data, $row); } fclose($data);
?>
Comments
Post a Comment