簡體   English   中英

如何使用 PHP 下載 CSV 文件

[英]How to download a CSV file using PHP

我的代碼的目的是從我的數據庫下載一個 CSV 文件。 但是,不是下載文件,而是將其內容顯示在網頁上。 此外,當我將下載 function 移動到我的 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,這將阻止發送標頭。

您應該將整個if ($_POST["action"] == "Find Duplicate") {... }塊移動到頁面的最頂部,並添加對exit(); array_to_csv_download() function 的末尾。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM