如何使用 PHP 下载 CSV 文件

How to download a CSV file using PHP

我的代码的目的是下载从我的数据库生成的 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>


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

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