页面无法通过强制csv下载加载

时间:2019-06-06 16:35:44

标签: php file pdo fopen

我有一个页面非常简单。有一个按钮,当按下该按钮时会强制下载CSV。除了一件事,那部分还在工作。该文件将在页面加载后立即下载。

为消除这一点,我添加了一个isset语句。除了现在,该页面没有加载。页面上有非常老的错误,绝对不应该显示。我什至创建了一个新文件并添加了代码。该页面甚至不会加载。文件刚刚下载,然后一切停止。

有人看到造成这种情况的原因吗?

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$file_name = "subscriber_list.csv";
$file = fopen("subscriber_list.csv","w");

try {
    $servername = 'localhost';
    $usernameCon = '';
    $passwordCon = '';
    $con = new PDO('mysql:host='.$servername.';dbname=', $usernameCon, $passwordCon);

    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql_subscribers = "
        SELECT first_name, last_name, phone, email, new_mowers, used_mowers, date_subscribed
        FROM test_notif
        ORDER BY date_subscribed
    ";
    $subscriber_stmt = $con->prepare($sql_subscribers);
    $subscriber_stmt->execute();
    $subscriber_rows = $subscriber_stmt->fetchAll(PDO::FETCH_ASSOC);

    if (isset($_POST['submitbutton'])) {
        foreach ($subscriber_rows as $subscriber_row) {
            //fputcsv($file,explode(',',$subscriber_row));
            fputcsv($file, $subscriber_row);
        }
    }

    header('Content-type: application/octet-stream');
    header("Content-Disposition: attachment; filename='.$file_name.'");

    fclose($file);
    readfile($file_name);
    exit;
}
catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}

?>
<body>
    <form action="" name="csvForm" method="POST">
        <input type="submit" value="Download File" name="submitbutton">
    </form>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

您可以尝试

<?php
if (isset($_POST['submitbutton'])) {
    error_reporting(E_ALL);
    ini_set('display_errors', 1);

    $file_name = "subscriber_list.csv";
    $file = fopen("subscriber_list.csv","w");
    try {
        $servername = 'localhost';
        $usernameCon = '';
        $passwordCon = '';
        $con = new PDO('mysql:host='.$servername.';dbname=', $usernameCon, $passwordCon);

        $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql_subscribers = " SELECT first_name, last_name, phone, email, new_mowers, used_mowers, date_subscribed
              FROM test_notif
              ORDER BY date_subscribed ";
        $subscriber_stmt = $con->prepare($sql_subscribers);
        $subscriber_stmt->execute();
        $subscriber_rows = $subscriber_stmt->fetchAll(PDO::FETCH_ASSOC);

        foreach ($subscriber_rows as $subscriber_row) {
            //fputcsv($file,explode(',',$subscriber_row));
            fputcsv($file, $subscriber_row);
        }

        header('Content-type: application/octet-stream');
        header("Content-Disposition: attachment; filename='.$file_name.'");

        fclose($file);
        readfile($file_name);
    }
     catch(PDOException $e) {
        echo "Connection failed: " . $e->getMessage();
    }
}
?>
<body>
    <form action="" name="csvForm" method="POST">
        <input type="submit" value="Download File" name="submitbutton">
    </form>
</body>
</html>