Force download file with PHP giving empty file

israrshah

New Member
The end goal is for the user to download a .csv file. Right now I'm just testing trying to download a simple text file: test.txt. The only thing in this file is the word "test".Here is the HTML code for files_to_download.php\[code\]Test file: <a href='http://stackoverflow.com/questions/3563769/test.php?file=test.txt'>Test.txt</a>\[/code\]Code for test.php:\[code\]if(!(empty($_GET["file"]))){ $file_name = $_GET["file"]; $path = "path/to/file"; $fullPath = $path . $file_name; if(ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off'); header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Disposition: inline; attachment; filename=\"$file_name\""); header("Content-Type: text/plain"); header("Content-Transfer-Encoding: binary"); readfile($fullPath);}\[/code\]I've tried variations of the headers above, adding more and removing others. The above seem to be the most common recommended on this site.I've also tried changing\[code\]header("Content-Type: text/plain");\[/code\]to\[code\]header("Content-Type: text/csv");\[/code\]and get same results: empty .txt or .csv file.The files are not empty when I open them directly (file browser) from the server. I've checked the permissions of the files and they're both 644, so the entire world can at least read the files. The directory is 777.Is there a configuration on the Apache server I need to specify that may not be or am I missing something above.Thanks for looking!
 
Back
Top