【问题标题】:CSV file displays content in browser instead of downloading from browserCSV 文件在浏览器中显示内容,而不是从浏览器下载
【发布时间】:2014-07-16 06:49:46
【问题描述】:

我想从我的数据库中的一些结果创建一个 csv 文件。但它显示内容而不是提示下载。

我知道对此有很多问题,但我没有运气。

 if(isset($_POST['export'])){

     $resultExport = mysql_query("SELECT question.id, question.question, answer, A, B, C, D, question.publish
                                  FROM question
                                  INNER JOIN packagequestion
                                  ON question.id = packagequestion.questionid
                                  WHERE packagequestion.packageid = '".$packageID."' AND question.publish = 1
                                  ORDER BY question.id DESC"); 

    if (!$resultExport) die('Couldn\'t fetch records');

            $num_fields = mysql_num_fields($resultExport); 
            $headers = array();

            for ($i = 0; $i < $num_fields; $i++) 
            {     
                $headers[] = mysql_field_name($resultExport , $i); 
            }

                $fp = fopen('php://output', 'w');

            if ($fp && $resultExport) 
            {
                // name the file by current category and package
                $filename = $categorie."".$packageName;     
                header('Content-Type: text/csv');
                header('Content-Disposition: attachment; filename='.$filename.".csv");
                header('Pragma: no-cache');    
                header('Expires: 0');
                fputcsv($fp, $headers);

                while ($row = mysql_fetch_row($resultExport)) 
                {
                    fputcsv($fp, array_values($row)); 
                }
            die; 
        }    





}

【问题讨论】:

    标签: php sql csv download


    【解决方案1】:
       hope this will help you
    
    
       <?php
    
       include 'connection.php'; 
       // Fetch Record from Database
    
       $output = "";
       $sql = mysql_query("select* from tablename");//your query
       $columns_total = mysql_num_fields($sql);
       // Get The Field Name
    
       for ($i = 0; $i < $columns_total; $i++) {
       $heading = mysql_field_name($sql, $i);
       $output .= '"'.$heading.'",';
       }
       $output .="\n";
    
       // Get Records from the table
    
      while ($row = mysql_fetch_array($sql)) {
      for ($i = 0; $i < $columns_total; $i++) {
      $output .='"'.$row["$i"].'",';
      }
      $output .="\n";
      }
    
      // Download the file
    
      $filename = "myFile.csv";
      header('Content-type: application/csv');
      header('Content-Disposition: attachment; filename='.$filename);
    
      echo $output;
      exit;
    
      ?>
    

    【讨论】:

    • 您是在同一个文件中还是在单独的文件中执行这些过程?
    • 我在另一个页面中有锚标记,同时单击该链接文件将下载...实际上你现在得到的
    【解决方案2】:

    为此,您只需从代码中删除以下行。

    header('Content-Type: text/csv');

    【讨论】:

      猜你喜欢
      • 2012-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多