从PHP服务器下载csv文件

时间:2016-11-26 08:15:24

标签: php mysql csv

我正在编写一个代码来保存我在csv文件中的查询输出。它将保存在服务器上的csv文件中。但是如何在按钮单击时从服务器下载该文件。我写了下面的代码,但它没有下载你的文件。任何人都可以帮助我。

代码: -

$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);

2 个答案:

答案 0 :(得分:0)

在php中生成csv文件的最佳方法

<?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>

答案 1 :(得分:0)

我用一些测试数据测试了你的代码:

<?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);

它已下载名为books.csv的文件。因此,下载文件有效。

内容:

<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

查看内容,开头有一些(不需要的)HTML。我认为这是你的问题。正确?

添加标题header('Content-Disposition: attachment; filename="books.csv"');告诉浏览器将输出作为要下载的文件处理。 所有输出都添加到文件中。因此,还会添加循环内echo的输出。如果我是对的,你只需要删除输出就应该在那里。