How to generate file for download then redirect using php?

0 votes

I have a PHP app that creates a CSV file which is forced to download using headers. Here's the relevant part of the code:

header('Content-Type: application/csv'); 
header("Content-length: " . filesize($NewFile)); 
header('Content-Disposition: attachment; filename="' . $FileName . '"'); 
echo $content;
exit(); 

What I'd like to do is redirect users to a new page after the file is built and the download prompt is sent. Just adding header("Location: /newpage") to the end didn't work, expectedly, so I'm not sure how to rig this up.

Oct 30 in PHP by kartik
• 35,500 points
67 views

1 answer to this question.

0 votes

Hii,

I don't think this can be done - although I am not 100% sure.

The common thing (e.g. in popular download sites) is the reverse: first you go to the after page and then the download starts.

So redirect your users to the final page that (among other things) says:

Your download should start automatically. If not click [a href="create_csv.php"]here[/a].

As about initiating the download (e.g. automatically calling create_csv.php) you have many options:

Thank you!!
answered Oct 30 by Niroj
• 76,940 points

Related Questions In PHP

0 votes
2 answers

How to copy a file from one directory to another using PHP?

Simply, $source = 'Source_file_location' ...READ MORE

answered Oct 12 in PHP by anonymous
• 140 points
46 views
0 votes
1 answer

How to create and download a csv file from php script?

Hello @kartik, You can use the built in fputcsv() for ...READ MORE

answered Oct 27 in PHP by Niroj
• 76,940 points
44 views
0 votes
1 answer

How to check if a file exists from a url using php?

Hello, You have to use CURL function does_url_exists($url) { ...READ MORE

answered Nov 3 in PHP by Niroj
• 76,940 points
68 views
0 votes
1 answer

How can one check to see if a remote file exists using PHP?

Hello @kartik, You can instruct curl to use ...READ MORE

answered Nov 10 in PHP by Niroj
• 76,940 points
26 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18 in Laravel by Niroj
• 76,940 points
3,470 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18 in Laravel by Niroj
• 76,940 points
412 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23 in Laravel by Niroj
• 76,940 points
286 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23 in Laravel by Niroj
• 76,940 points
4,167 views
0 votes
1 answer

How to copy a file from one directory to another using PHP?

Hello @kartik, You could use the copy() function : // Will ...READ MORE

answered Sep 15 in PHP by Niroj
• 76,940 points
47 views
0 votes
1 answer

How to generate .json file with PHP?

Hello @kartik, Here is a sample code: <?php $sql="select ...READ MORE

answered Sep 15 in PHP by Niroj
• 76,940 points
59 views