Search code examples
phpfilepdofopen

Page not loading with forced csv download


I have a page in which is very simple. There is a button that when pressed forces a CSV to download. That part was working except for one thing. The file would download immediately on page load.

To eliminate that I added an isset statement. Except now, the page isn't loading. There are very old errors on the page that should definitely not be showing. I even created a new file and added the code. The page won't even load. The file just downloads and then everything stops.

Does anyone see what could be causing this?

<?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>

Solution

  • You could try

    <?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>