【问题标题】:How to download a CSV file using PHP如何使用 PHP 下载 CSV 文件
【发布时间】:2020-11-12 17:13:44
【问题描述】:

我的代码的目的是从我的数据库中下载一个 CSV 文件。但是,不是下载文件,而是将其内容显示在网页上。此外,当我将下载功能移动到我的 for 循环中时,它会下载 CSV 文件,但我无法访问网页。这是整个代码:

<html>
    <style>
        table, th, td{
            padding: 10px;
            border: 1px solid black;
            border-collapse: collapse;
        }
        table, .center{
            text-align: center;
            margin-left: auto;
            margin-right: auto;
        }
    </style>

    <?php

    function array_to_csv_download($array, $filename = "export.csv", $delimiter = ",") {
        // open the "output" stream
        // see http://www.php.net/manual/en/wrappers.php.php#refsect2-wrappers.php-unknown-unknown-unknown-descriptioq
        header("Content-disposition: attachment; filename=test.csv");
        header("Content-Type: text/csv");

        ob_clean();
        flush();
        $f = fopen('php://output', 'w');
        foreach ($array as $line) {
            fputcsv($f, $line, $delimiter);
        }
        readfile($f);
        ob_end_clean();
    }

    mysql_connect("localhost", "root", "");
    mysql_select_db("gograb");
    $filename = $_FILES["file"]["tmp_name"];
    echo "<table id='center'>"
    . "<tr>"
    . "<th>"
    . "Number"
    . "</th>"
    . "<th>"
    . "Brand"
    . "</th>"
    . "<th>"
    . "Item Name"
    . "</th>"
    . "<th>"
    . "Description"
    . "</th>"
    . "<th>"
    . "Weight"
    . "</th>"
    . "<th>"
    . "Variant1 Name"
    . "</th>"
    . "<th>"
    . "Variant1 Value"
    . "</th>"
    . "<th>"
    . "Variant2 Name"
    . "</th>"
    . "<th>"
    . "Variant2 Value"
    . "</th>"
    . "<th>"
    . "Price"
    . "</th>"
    . "<th>"
    . "Barcode"
    . "</th>"
    . "<th>"
    . "Department"
    . "</th>"
    . "<th>"
    . "Collections"
    . "</th>"
    . "<th>"
    . "Type"
    . "</th>"
    . "<th>"
    . "Tag"
    . "</th>"
    . "<th>"
    . "Image URL"
    . "</th>"
    . "</tr>";
    if ($_POST["action"] == "Find Duplicate") {
        if (($h = fopen("{$filename}", "r")) !== FALSE) {
            $skip_var = 1;
            while (($data = fgetcsv($h, 30000, ",")) !== FALSE) {
                if ($skip_var == 1) {
                    $skip_var += 1;
                    continue;
                }
                echo "<tr>";
                $select = "SELECT * FROM global_catalog where Barcode='$data[10]'";
                $query = mysql_query($select);
                if (mysql_num_rows($query) > 0) {
                    $row = mysql_fetch_assoc($query);
                    foreach ($data as $field => $value) {
                        echo "<td>" . $value . "</td>";
                    }
                    echo "</tr>";
                }
                $big_array[] = $data;
                
            }
        }
        array_to_csv_download($big_array);
    }
    $counter = 0;
    if ($_POST["action"] == "Find New") {
        if (($h = fopen("{$filename}", "r")) !== FALSE) {
            $skip_var = 1;
            while (($data = fgetcsv($h, 30000, ",")) !== FALSE) {
                if ($skip_var == 1) {
                    $skip_var += 1;
                    continue;
                }
                echo "<tr>";
                $select = "SELECT * FROM global_catalog where Barcode='$data[10]'";
                $query = mysql_query($select);
                if (mysql_num_rows($query) == 0) {
                    $counter+=1;
                    $row = mysql_fetch_assoc($query);
                    foreach ($data as $field => $value) {
                        echo "<td>" . $value . "</td>";
                    }
                    echo "</tr>";
                }
            }
            fclose($h);
        }
    }
    echo "</table>";
    ?>
</html>


【问题讨论】:

    标签: php sql csv download web-development-server


    【解决方案1】:

    您的页面顶部正在输出 HTML,这将阻止标题发送。

    您应该将整个if ($_POST["action"] == "Find Duplicate") { ... } 块移动到页面的最顶部,并在array_to_csv_download() 函数的末尾添加对exit(); 的调用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-04
      • 1970-01-01
      • 2020-12-29
      • 2015-05-03
      • 2015-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多