I get a file with no error found when using the PHP () header

I have a php file that is designed to download CSV files.

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

Where $csvOutis the line that I generated in another function.

The problem is that when I go to this PHP file, my browser gives me a 404 file that was not found. This does the same when I delete a row header('Content-Disposition: attachment;filename=users.csv');.

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

This really confuses me, as I have successfully used this code to work with CSV files before, and I don’t see what I am doing (or not doing) that violates it.

Any help would be greatly appreciated!

+4
2

:

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

,

0

200 OK. , Content-Type / Content-Disposition 404, , , script, , :

<?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();
0

All Articles