如何在php中生成后可以下载csv

时间:2014-06-13 07:58:23

标签: php csv download

我正在生成csv文档,csv文件正在生成但文件未下载。

我尝试过的代码:

这是我将数据从数据库表导出到csv文件

的函数
$file = export_excel_csv()

这是我尝试但下载的下载脚本

if (file_exists('payment_reports/'.$file)) {

                    //set appropriate headers
                    header('Content-Description: File Transfer');
                    header('Content-Type: application/csv');
                    header('Content-Disposition: attachment; filename='.basename($file));
                    header('Expires: 0');
                    header('Cache-Control: must-revalidate');
                    header('Pragma: public');
                    header('Content-Length: ' . filesize($file));
                    ob_clean();
                    flush();

                    //read the file from disk and output the content.
                    readfile($file);
                    exit;
                }

将数据放入excel的数据:

 function export_excel_csv()
    {
        $file_name = strtotime("now").".csv";
      $file_namepath = "payment_reports/".$file_name; // file name will be created here (originally file is created, as we are just fixing the name of the csv file)
       $fp = fopen($file_namepath,"w"); // opening the file to write the date, here in this step file will be created
       $sql_query = "MYQUERY";

       $result_getData = mysql_query($sql_query); 
       $row_data = mysql_fetch_assoc($result_getData); // as we are dealing with files, better to use mysql_fetch_assoc, we can also use mysql_fetch_array but better to use mysql_fetch_assoc

       $comma = ""; // initializing the variables
       $seperator = "";
       // this foreach to get the column name from the database and writes data to csv file
       foreach($row_data as $name => $value)
       {
           $seperator .= $comma . '' .str_replace('', '""',$name);   // $name will be having table column names.
           $comma = ",";
       }
       $seperator .= "\n";
       fputs($fp,$seperator); //puts the columns names in the csv file
       // ends writing headings into csv file

       mysql_data_seek($result_getData, 0); // leaves the first line in the csv file
       while($row_data = mysql_fetch_assoc($result_getData)) //fetching the table data to write into csv file
       {
            $comma = "";
            $seperator = "";

            foreach($row_data as $name => $value) 
            {
                $seperator .= $comma . '' .str_replace('', '""',$value); // $value will be having table data.
                $comma = ",";

            }
            echo 'usd : '.$row_data['usd'].'<br>';
            $total_usd += $row_data['usd'];
            $seperator .= "\n";

            fputs($fp,$seperator);
       }
       $seperator_usd .= ",";
       $seperator_usd .= ",";
       $seperator_usd .= "Total USD,";
       $seperator_usd .= $total_usd;
       fputs($fp,$seperator_usd);
       return $file_name;
    }

2 个答案:

答案 0 :(得分:0)

您可以在使用完整路径后检查代码,即使用

if (file_exists('payment_reports/'.$file)) {
                    //set appropriate headers
                    header('Content-Description: File Transfer');
                    header('Content-Type: application/csv');
                    header('Content-Disposition: attachment; filename='.basename('payment_reports/'.$file));
                    header('Expires: 0');
                    header('Cache-Control: must-revalidate');
                    header('Pragma: public');
                    header('Content-Length: ' . filesize('payment_reports/'.$file));
                    ob_clean();
                    flush();

                    //read the file from disk and output the content.
                    readfile('payment_reports/'.$file);
                    exit;
                }

答案 1 :(得分:0)

最后它正在运作

代码:

 <td><a href='download.php?filename=<?=$row['report_file_name'] ?>' alt='Click here to download report' target='_blank'><b>Download</b></a></td>

<强>的download.php

<?php
header('HTTP/1.1 200 OK');
header('Cache-Control: no-cache, must-revalidate');
header("Pragma: no-cache");
header("Expires: 0");
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=".$_REQUEST['filename']);
readfile('payment_reports/' . $_REQUEST['filename']);
exit;
?>