【问题标题】:Using PHP to generate CSV file from MySQLi is saving but not asking to download使用 PHP 从 MySQLi 生成 CSV 文件正在保存但不要求下载
【发布时间】:2015-07-20 22:29:01
【问题描述】:

因此,我的以下代码根据指定的表生成了一个 CSV,并生成文件并保存到 downloads/filename.csv,但是它不会在生成后要求用户下载。任何想法为什么?

代码如下:

PHP

    header("Content-type: text/csv"); 

        // Connect to the database
        $mysqli = new mysqli(DATABASE_HOST, DATABASE_USER, DATABASE_PASS, DATABASE_NAME);

        // output any connection error
        if ($mysqli->connect_error) {
            die('Error : ('.$mysqli->connect_errno .') '. $mysqli->connect_error);
        }

        $tables = array('invoices', 'customers', 'invoice_items'); // array of tables need to export

        $file_name = 'invoice-export-'.date('d-m-Y').'.csv';   // file name
        $file_path = 'downloads/'.$file_name; // file path

        $file = fopen($file_path, "w"); // open a file in write mode
        chmod($file_path, 0777);    // set the file permission

        // loop for tables
        foreach($tables as $table) {
            $table_column = array();
            $query_table_columns = "SHOW COLUMNS FROM $table";

            // fetch table field names
            if ($result_column = mysqli_query($mysqli, $query_table_columns)) {
                while ($column = $result_column->fetch_row()) {
                    $table_column[] = $column[0];
                }
            }

            // Format array as CSV and write to file pointer
            fputcsv($file, $table_column, ",", '"');

            $query_table_columns_data = "SELECT * FROM $table";

            if ($result_column_data = mysqli_query($mysqli, $query_table_columns_data)) {

                // fetch table fields data
                while ($column_data = $result_column_data->fetch_row()) {
                    $table_column_data = array();
                    foreach($column_data as $data) {
                        $table_column_data[] = $data;
                    }

                    // Format array as CSV and write to file pointer
                    fputcsv($file, $table_column_data, ",", '"');
                }
            }
}

    // close file pointer
    fclose($file);

    // ask either save or open
    header("Pragma: public");
    header("Expires: 0");
    header("Content-Type: application/octet-stream");
    header("Content-Disposition: attachment; filename='{$file_name}';" );
    header("Content-Transfer-Encoding: binary");

    // open a saved file to read data
    $fhandle = fopen($file_path, 'r');
    fpassthru($fhandle);
    fclose($fhandle);
    $mysqli->close();
    die;

【问题讨论】:

标签: php mysql csv mysqli


【解决方案1】:

这应该允许您选择要写入 CSV 文件的字段及其顺序。

// loop over the rows, outputting them
while($row = $results->fetch_assoc()) {
    $data = [
       $row["myfirstfield]",
       $row["mysecondfield"],
       ....
       ];
    fputcsv($output, $data);
 }

【讨论】:

  • 谢谢伙计,我现在就试试,让你知道。
  • PHP 解析错误:语法错误,第 52 行 /home/ambnews/public_html/invoice/response.php 中出现意外 '[',
  • 我猜你使用的是旧版本的 PHP..... 将 $data = [....] 替换为 $data = array(....) 或从旧版本的 PHP (
  • 好的,我改变了它,但得到:PHP Parse error: syntax error, unexpected ',', expecting ']' in /home/ambnews/public_html/invoice/response.php on line 79 - 生病了立即使用最新代码更新问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-14
  • 1970-01-01
  • 2017-11-14
  • 1970-01-01
相关资源
最近更新 更多