【问题标题】:php fputcsv not workingphp fputcsv 不工作
【发布时间】:2017-01-25 14:53:02
【问题描述】:

我不明白为什么我的fputcsv 不起作用。这是我所拥有的:

<?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    $id = $_GET['Id'];
    include('DBConn.php');
    $query = $conn->prepare('SELECT QName, tsql from pmdb.QDefs WHERE Id = ' . $id);
    $query->execute();
    $qdef = $query->fetch(PDO::FETCH_ASSOC);

    // Create and open file for writing
    $filepath = 'exports/';
    $filename = $qdef['QName'] . '.csv';
    //$filename = $qdef['QName'] . '.csv';
    try
    {
        $openFile = fopen($filepath . $filename,'w');
        header('Content-Encoding: UTF-8');
        header('Content-Type: text/csv; charset:UTF-8');
        header('Content-Disposition: attachment; filename="' . $filename . '"');
    }
    catch(Exception $e)
    {
        echo "Something went wrong<br>";
        die( print_r( $e->getMessage()));
    }

    // Use returned tsql field as query for dataset
    $tsql = $qdef['tsql'];
    //echo "tsql<br>"; print_r($tsql); //This was to make sure that I'm getting the correct query
    $query = $conn->prepare($tsql);
    $query->execute();

    // Output data to CSV file
    $headers = NULL;
    while ($row = $query->fetch(PDO::FETCH_ASSOC))
    {
        //Write column headings to file
        if (is_null($headers))
        {
            $headers = array_keys($row);

            if ($headers[0] == 'ID')
                $headers[0] = 'Id';
            fputcsv($openFile, $headers); //This is doing nothing The headers from here don't get printer to the file
            //print_r($headers); //This was to check and see if the headers exist
            print_r($openFile);echo ","; //This is to see what is returned from the fopen -> it returns "Resource is #4"
            foreach($headers as $Header)
            {
                echo $Header. ","; //This was to print the headers to see if I can write to the file. I can the headers print.
            }
        }
        //Write data
        $modRow = preg_replace('/ \d{2}:\d{2}:\d{2}\.\d{3}/', '', $row);
        /*
        $modRow = str_replace('\r\n', " ", $modRow);
        $modRow = str_replace('\n\r', " ", $modRow);
        $modRow = str_replace('\n', " ", $modRow);
        $modRow = str_replace('\r', " ", $modRow);
        */
        fputcsv($openFile, $modRow, ',','"');//print_r($modRow); //The rows don't get added to the file from here like they should, but I also don't get any error.
    }

    // Close file
    fclose($openFile);

    //echo $filepath . $filename;
?>

没有错误它只是导出一个空白文件,尽管它确实命名正确。对于大多数报告,该文件应包含 30 多列和 10000 多行。

这是我的错误报告的设置方式:

    error_reporting(E_ALL|E_STRICT);//For use in trouble shooting
    ini_set('display_errors', 'On');//For use in trouble shooting

【问题讨论】:

  • 定义“不工作”。有任何错误消息、警告、通知吗?你得到了什么和你期望什么?等等。如果您的 error_reporting 设置至少没有设置为 E_ALL,请先修复它。
  • 创建 MVCE 也很有帮助。关于 fgetvsv() 的问题不需要 SQL 或弄乱 HTTP 标头。
  • @cHao 没有错误,它只是导出一个空白文件。这就是为什么Om感到困惑。如果出现错误,我会有一些事情要追查,但文件只是空白,我可以echoprint_r 这样做,所以我知道它被打开了。只是 fputcsv 似乎不起作用。
  • 开始调试你的代码。您的查询是否返回任何数据?如果你在你的while 语句后面加上一个die('entering while');,你的代码会到达那个部分吗?
  • 您是否尝试过 print_r() 数据以确保它不为空?您也可以使用增量变量来检查它运行了多少次。如果它正在运行,但没有显示任何数据,那么数据有问题。

标签: php fopen export-to-csv fputcsv


【解决方案1】:

我最终创建了一个解决方法,因为 In 无法让 fputcsv 工作。

我现在正在这样做:

foreach($modRow as $RowPrint)
{
    echo '"' .trim(unserialize(serialize($RowPrint))). '"' .$sep;
}
echo $br;

$sep = ","$br = "\r\n" 在哪里

【讨论】:

    猜你喜欢
    • 2016-09-17
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多