dswy34539
2016-05-16 09:56
浏览 107
已采纳

使用PHP导出数据后下载的文件为空

I have this PHP script to update a specific table of MySQL db:

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
//Include connection file
require_once('../connection/connect.php');

header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=reports.csv");
header("Pragma: no-cache");
header("Expires: 0");

$sql = "SELECT * FROM patient";
$stmt = $conn->prepare($sql);

$stmt->execute();
//var_dump($stmt->fetch(PDO::FETCH_ASSOC));
$data = fopen('backup.csv', 'w');
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
    // Export every row to a file
    fputcsv($data, $row);
}
?>

I created a file in the same directory called: backup.csv and the data are updated correctly each time I execute this script. But the problem is that I can't add a header to table inside the .csv file.

And the downloaded file is empty. I need to download backup.csv and not reports.csv.

I tried:

header("Content-Disposition: attachment; filename="'.$data.'";");

But it didn't helped. The file is still empty.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongtuhe0506 2016-05-16 10:07
    已采纳

    Your problem is that you have not actually sent the file that you have created.

    This is a better structure but remember you also have to sent the file you have built to the browser

    <?php
    error_reporting(E_ALL);
    ini_set("display_errors", 1);
    //Include connection file
    require_once('../connection/connect.php');
    
    $sql = "SELECT * FROM patient";
    $stmt = $conn->prepare($sql);
    
    $stmt->execute();
    
    $data = fopen('backup.csv', 'w');
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
    {
        // Export every row to a file
        fputcsv($data, $row);
    }
    fclose($data);                 // <- close file
    
    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=reports.csv");
    header("Pragma: no-cache");
    header("Expires: 0");
    readfile('backup.csv');        // <- new line
    ?>
    
    点赞 评论
  • doudichu1358 2016-05-16 09:59

    You should do fclose($data); right after while loop. I recommend you to use headers after execution of your query code. Please try/see the following example code.

    Example

    <?php
    error_reporting(E_ALL);
    ini_set("display_errors", 1);
    //Include connection file
    require_once('../connection/connect.php');
    
    $sql = "SELECT * FROM patient";
    $stmt = $conn->prepare($sql);
    
    $stmt->execute();
    //var_dump($stmt->fetch(PDO::FETCH_ASSOC));
    $data = fopen('backup.csv', 'w');
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
    {
        // Export every row to a file
        fputcsv($data, $row);
    }
    // Closing the file
    fclose($data);
    
    
    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=backup.csv");
    header("Pragma: no-cache");
    header("Expires: 0");
    ?>
    
    点赞 评论

相关推荐 更多相似问题