简体   繁体   中英

Download csv file from PHP server

I am writing a code to save my query output in csv file. It is getting saved in the csv file on the server. But how to download that file from the server on button click. I have written following code but it is not downloading thee file. Can any one help me out.

Code:-

$group_result = mysql_query($selectquery, $conn);                                                   
$fp = fopen('books.csv', 'w');
header('Content-Type: application/csv; charset=UTF-8');
header('Content-Disposition: attachment; filename="books.csv"');
$filename = 'books.csv';
while ($container_id_record = mysql_fetch_assoc($group_result)) {
$i++;

echo"<tr class='gradeA odd' role='row'><td class='sorting_1'>" . $container_id_record['enquiry_date'] . "</td><td>" . $container_id_record['name'] . "</td><td>" .$container_id_record['mobile']. "</td><td>" . $container_id_record['program_name'] .  "</td><td>" . $container_id_record['schedule'] .  "</td><td>" . $container_id_record['shift'] .  "</td><td>" . $container_id_record['remark'] .  "</td>";

fputcsv($fp, $container_id_record);


}

fclose($fp);
readfile($filename);

Best way to generate a csv file in php

<?php 
$list = array (
array('aaa', 'b,bb', 'cc\c', 'ddd/d'),
array('123', '45,6', '789'),
array('"aaa"', '"bbb"'));
$fp = fopen('file.csv', 'w+');
foreach ($list as $fields) {
    fputcsv($fp, $fields);
}
fclose($fp);
?>

<a href="file.csv">Download File</a>

I tested your code with some test data:

<?php

ini_set('html_errros', 0);
$i = 0;
//$group_result = mysql_query($selectquery, $conn);


$group_result = array(
    array(
        'enquiry_date' => 1, 'name' => 1, 'mobile' => 1, 'program_name' => 1, 'schedule' => 1, 'shift' => 1, 'remark' => 1,
    ),
    array(
        'enquiry_date' => 2, 'name' => 2, 'mobile' => 2, 'program_name' => 2, 'schedule' => 2, 'shift' => 2, 'remark' => 2,
    ),
    array(
        'enquiry_date' => 3, 'name' => 3, 'mobile' => 3, 'program_name' => 3, 'schedule' => 3, 'shift' => 3, 'remark' => 3,
    ),
);


$fp = fopen('books.csv', 'w');
header('Content-Type: application/csv; charset=UTF-8');
header('Content-Disposition: attachment; filename="books.csv"');
$filename = 'books.csv';
//while ($container_id_record = mysql_fetch_assoc($group_result)) {
foreach ($group_result as $container_id_record) {
    $i++;

    echo"<tr class='gradeA odd' role='row'><td class='sorting_1'>".$container_id_record['enquiry_date']."</td><td>".$container_id_record['name']."</td><td>".$container_id_record['mobile']."</td><td>".$container_id_record['program_name']."</td><td>".$container_id_record['schedule']."</td><td>".$container_id_record['shift']."</td><td>".$container_id_record['remark']."</td>";

    fputcsv($fp, $container_id_record);
}

fclose($fp);
readfile($filename);

It already downloads a file named books.csv . So, downloading a file works.

Contents:

<tr class='gradeA odd' role='row'><td class='sorting_1'>1</td><td>1</td><td>1</td><td>1</td><td>1</td><td>1</td><td>1</td><tr class='gradeA odd' role='row'><td class='sorting_1'>2</td><td>2</td><td>2</td><td>2</td><td>2</td><td>2</td><td>2</td><tr class='gradeA odd' role='row'><td class='sorting_1'>3</td><td>3</td><td>3</td><td>3</td><td>3</td><td>3</td><td>3</td>1,1,1,1,1,1,1
2,2,2,2,2,2,2
3,3,3,3,3,3,3

Looking at the contents, there is some (unwanted) HTML in the beginning. I think that's your problem. Right?

Adding the header header('Content-Disposition: attachment; filename="books.csv"'); tells the browser to handle the output as a file to download. All output is added to the file. So, the output from your echo inside your loop is also added. If I'm right, you just have to remove the output and you should be there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM