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

时间:2014-09-22 11:18:20

标签: php mysql csv download dynamically-generated

我编写了以下脚本,即根据数据库中的内容创建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);

?>

2 个答案:

答案 0 :(得分:2)

在进一步测试并找到关于该主题的类似帖子之后,我找到了一个修复程序。我没有在文件上使用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);

?>

答案 1 :(得分:0)

尝试把这个

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