【问题标题】:CSV export without MYSQL FILE functions没有 MYSQL 文件功能的 CSV 导出
【发布时间】:2012-06-18 15:40:23
【问题描述】:

我想导出为 CSV 格式,但我使用的互斥主机已停用 mysql 中的 FILE 功能。

我做了一个简单的 SELECT,然后用 PHP 进行了 fopen 和 fwrite。

问题是字段中有回车或双引号。

如何保存它们并构建正确的 csv 文件?

非常感谢。

【问题讨论】:

标签: php mysql csv select-into-outfile


【解决方案1】:

构建最佳 CSV。您可以按照以下方式进行。

   $filename ='data.csv';
   $csv_terminated = "\n";
   $csv_separator = ",";
   $csv_enclosed = '"';
   $csv_escaped = "\\";

$results  = array('1','2','3');// value

    $schema_insert = '';

    $header = array('a','b','c');// header


    for ($i = 0; $i< count($header); $i++)
    {
        $l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
            stripslashes($header[$i])) . $csv_enclosed;
        $schema_insert .= $l;
        $schema_insert .= $csv_separator;
    } // end for

    $out = trim(substr($schema_insert, 0, -1));
    $out .= $csv_terminated;

    // Format the data
    for($i=0;$i<count($results);$i++)
    {
        $row = $results[$i];
        $schema_insert = '';
        for ($j = 0; $j < count($header); $j++)
        {
            if ($row[$j] == '0' || $row[$j] != '')
            {

                if ($csv_enclosed == '')
                {
                    $schema_insert .= $row[$j];
                } else
                {
                    $schema_insert .= $csv_enclosed .
                    str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed;
                }
            } else
            {
                $schema_insert .= 'NULL';
            }

            if ($j < count($header) - 1)
            {
                $schema_insert .= $csv_separator;
            }
        } // end for

        $out .= $schema_insert;
        $out .= $csv_terminated;
    } // end while

    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Length: " . strlen($out));
    // Output to browser with appropriate mime type, you choose <img src="http://thetechnofreak.com/wp-includes/images/smilies/icon_wink.gif" alt=";)" class="wp-smiley">
    header("Content-type: text/x-csv");
    //header("Content-type: text/csv");
    //header("Content-type: application/csv");
    header("Content-Disposition: attachment; filename=$filename");
    echo $out;

请注意, + 当您为包含 html 代码的描述添加附件时,您应该使用双引号。 + 空值 --> 更改为 Null 文本或零值

它们将使您的 CSV 更好。

【讨论】:

  • 欢迎您。请停止使用“垃圾邮件”标记您的答案。这是不受欢迎的。也请阅读this post 关于这个话题。享受您在这里的进一步住宿。
【解决方案2】:

你可以使用 header() 下载 file.csv

在header函数之后输出你想要的一切(基于csv格式),例如:

    $filename = "output";
    header('Content-Type: text/csv');
    header('Content-disposition: attachment;'.$filename.'=.csv');

    $separate = ","; //or ;
    $endline = "\r\n";
    foreach($data as $key => $item)
    {
        echo $key.$separate.$value.$endline;
    }

【讨论】:

    【解决方案3】:
    protected function getCsv( $fileName, array $data )
        {
            // alot of headers here, make force download work on IE8 over SSL
            header("Pragma: public");
            header("Expires: 0");
            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
            header("Cache-Control: private",false);
            header("Content-Type: application/octet-stream");
            header('Content-Disposition: attachment; filename="'.$fileName.'"');
            header("Content-Transfer-Encoding: binary"); 
    
            // Joe Green says:
            // based on http://www.php.net/manual/en/function.fputcsv.php#100033
    
            $outStream = fopen("php://output", "r+");
            function __getCsv( &$vals, $key, $filehandler ) {
                fputcsv( $filehandler, $vals, ',', '"');
            }
            array_walk( $data, '__getCsv', $outStream );
            fclose($outStream);
        }
    

    fputcsv 是你的朋友。此外,我必须改进这个功能才能达到这一点。 IE 需要其中一些标头来强制 csv 作为 csv 打开,尤其是通过 SSL。我似乎记得这与 IE8 在第一个实例中无法识别“text/csv”内容类型以及在第二个实例中围绕 SSL 下载的一些安全功能有关。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-05
      • 1970-01-01
      • 1970-01-01
      • 2017-11-02
      • 2014-05-12
      • 1970-01-01
      • 2023-03-13
      • 2014-10-20
      相关资源
      最近更新 更多