4

I have a php file which is meant to serve a CSV download.

header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=users.csv');
echo $csvOut;

Where $csvOut is a string I generated in another function.

The problem is that when I navigate to this PHP file, my browser gives me a 404 file not found error. It does the same thing when I remove the header('Content-Disposition: attachment;filename=users.csv'); line as well.

However, I noticed that if I change header('Content-Type: text/csv'); to header('Content-Type: text/html'); it displays the contents of $csvOut on the screen but ONLY if header('Content-Disposition: attachment;filename=users.csv'); has been removed.

This is really confusing me as I have successfully used this code to serve CSV files before and I can't see what I'm doing (or not doing) which is breaking it.

Any help would be greatly appreciated!

6
  • Was the string generated before you set the header or after it was set? I'm asking because I had the same problem as you described and the source of the problem turned out to be an exception being thrown (I had an error in SQL query) after I've already set the header for a CSV file. Because the header was already set for the CSV file, the browser was getting confused as to what it received, when in fact my script returned good old error code 500 html. I was able to figure it out by executing an ajax request in the console and looking at the responseText. Commented May 20, 2015 at 12:40
  • Additionally different browsers behaved differently with this problem. It was file not found in Firefox, but in Chrome it was ERR_INVALID_RESPONSE. Commented May 20, 2015 at 13:12
  • @jahu, Hmmm, interesting, good idea to check it out with AJAX - I'll try it out. Thanks for the response! :D Commented May 20, 2015 at 23:15
  • can you download a .csv file NOT generated by the php? You will need to make sure that .csv is an allowed mime type on the server (the types of file the server is permitted to return). Commented Jun 1, 2015 at 13:17
  • @JonHolland, Yes it is an allowed MIME type - I have directly accessed a csv file sitting on the server and it downloads with no problems. Commented Jun 1, 2015 at 23:16

2 Answers 2

1

Have you tried this:

header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=users.csv');
echo $csvOut;
exit(); //Stop script from processing anything else and this will trigger the download

Hope this helps

Sign up to request clarification or add additional context in comments.

Comments

1

Try setting the 200 OK response first. I don't know why Content-Type and/or Content-Disposition headers returns a 404 not found when the file actually exists, but it made my script to start the download, so it might work for you too:

<?php
header($_SERVER['SERVER_PROTOCOL'].' 200 OK');
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename=users.csv');

echo file_get_contents($csvOut);
exit();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.