生成的CSV下载为空,但有内容


Generated CSV downloads empty but has content

我已经编写了以下脚本,该脚本将基于数据库中的内容创建CSV文件。脚本本身工作得很好,可以创建CSV文件并按预期进行填充。问题是,当文件自动下载时,它是空的,但当通过FTP从托管服务器下载时,文件中充满了信息。

在成功写入文件之前,文件是否下载过快?有什么办法可以解决这个问题吗?

<?php
    // Establish the MySQL Database Connection
    include_once("./include/database.php");
    include("functions.php");
    $filename = 'devices.csv';
    $headers = array('ID', 'Device', 'Name', 'Type', 'Scope', 'OS', 'Datacenter');
    $handle = fopen($filename, 'w');
    fputcsv($handle, $headers, ',', '"');
    $sql = mysql_query("SELECT * FROM devices ORDER BY name ASC", $dp_conn);
    while($results = mysql_fetch_object($sql))
    {
        $type = getDeviceType($results->type, $dp_conn);
        $scope = getDeviceScope($results->scope, $dp_conn);
        $os = getOS($results->os, $dp_conn);
        $datacenter = getDatacenter($results->datacenter, $dp_conn);
        $row = array(
            $results->id,
            $results->device_id,
            $results->name,
            $type,
            $scope['name'],
            $os,
            $datacenter
        );
        fputcsv($handle, $row, ',', '"');
    }
    // rewind the "file" with the csv lines
    fseek($handle, 0);
    header('Content-Type: application/csv');
    header('Content-Disposition: attachment; filename="' . $filename . '";');
    // make php send the generated csv lines to the browser
    fpassthru($handle);
    fclose($handle);
?>

在进一步测试并找到关于该主题的类似帖子后,我找到了修复方法。我没有在文件上使用fopen(),而是将数据写入内存,现在它可以正常工作了。

<?php
    // Establish the MySQL Database Connection
    include_once("./include/database.php");
    include("functions.php");
    $filename = 'devices.csv';
    $headers = array('ID', 'Device', 'Name', 'Type', 'Scope', 'OS', 'Datacenter');
    //$handle = fopen($filename, 'w');
    $handle = fopen('php://memory', 'w'); 
    fputcsv($handle, $headers, ',', '"');
    $sql = mysql_query("SELECT * FROM devices ORDER BY name ASC", $dp_conn);
    while($results = mysql_fetch_object($sql))
    {
        $type = getDeviceType($results->type, $dp_conn);
        $scope = getDeviceScope($results->scope, $dp_conn);
        $os = getOS($results->os, $dp_conn);
        $datacenter = getDatacenter($results->datacenter, $dp_conn);
        $row = array(
            $results->id,
            $results->device_id,
            $results->name,
            $type,
            $scope['name'],
            $os,
            $datacenter
        );
        fputcsv($handle, $row, ',', '"');
    }
    // rewind the "file" with the csv lines
    fseek($handle, 0);
    header('Content-Type: application/csv');
    header('Content-Disposition: attachment; filename="' . $filename . '";');
    // make php send the generated csv lines to the browser
    fpassthru($handle);
    fclose($handle);
?>

尝试放置此

// Establish the MySQL Database Connection
include_once("./include/database.php");
include("functions.php");
ob_start(); //start output buffering 
$filename = 'devices.csv';
$headers = array('ID', 'Device', 'Name', 'Type', 'Scope', 'OS', 'Datacenter');
$handle = fopen($filename, 'w');
fputcsv($handle, $headers, ',', '"');
$sql = mysql_query("SELECT * FROM devices ORDER BY name ASC", $dp_conn);
while($results = mysql_fetch_object($sql))
{
    $type = getDeviceType($results->type, $dp_conn);
    $scope = getDeviceScope($results->scope, $dp_conn);
    $os = getOS($results->os, $dp_conn);
    $datacenter = getDatacenter($results->datacenter, $dp_conn);
    $row = array(
        $results->id,
        $results->device_id,
        $results->name,
        $type,
        $scope['name'],
        $os,
        $datacenter
    );
    fputcsv($handle, $row, ',', '"');
}
ob_end_clean(); //ending output buffering. 
// rewind the "file" with the csv lines
fseek($handle, 0);
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename="' . $filename . '";');
// make php send the generated csv lines to the browser
fpassthru($handle);
fclose($handle);