My Force Download Code isn't working correctly

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
KatieMcW
New php-forum User
New php-forum User
Posts: 2
Joined: Tue May 22, 2018 9:42 am

Tue May 22, 2018 9:58 am

Hi

I have a php file called "list.php" that displays a list of all tenders and allows the Administrator to filter the tenders based on 3 select drop downs (status, type and year).

I have code inside here that writes these filtered results to a file called "tenders.csv". This file is re-created each time a new filter selection is made.

At the bottom of my page I have added an href with a link to my "download.php" page which forces the download of this file.

When I click the href on the "list.php" page, a pop up box displays with the options of Open with or Save File. The file isn't my tenders.csv file though, it's a php file (see attached screenshot).

Not sure how to fix this.

Here is the href in the "list.php" file.

Code: Select all

        <?php
 	$stmp1 = WP_PLUGIN_URL . TENDER_FOLDER . "/download.php";
            ?>
           <a href="<?php echo $stmp1; ?>">Download CSV</a>
Attachments
download-file.png
download-file.png (20.5 KiB) Viewed 274 times
sample-csv.png
sample-csv.png (4.84 KiB) Viewed 274 times
popup-box.png
popup-box.png (20.08 KiB) Viewed 274 times

element121
php-forum Fan User
php-forum Fan User
Posts: 628
Joined: Sat Jun 06, 2015 11:46 am
Contact:

Wed May 30, 2018 10:36 am

Hi,

Try changing your Content-Type to this:

Code: Select all

    header('Content-Type: application/csv');
Hope that helps,
Jon

Post Reply