stuartbates Posted August 20, 2010 Share Posted August 20, 2010 I'm creating a CSV file on the fly for download in the backend - a stock management function. The script works fine extracting from the database fine and creates the CSV. But the next step of the script is to download the file using: header('Content-type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . $filename . '"'); This works, in as much as it downloads the file specified by $filename but when I open the file it's empty and the File Size in properties is 0. But if I go into my FTP and download the file it contains all the data as expected? Anyone have any ideas whats wrong? Thoughts I've had: File size too large would using ob_start('ob_gzhandler'); help? Or is there an INI setting? File encoding needs to be set? UTF8? Content Type should be text/csv? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/211276-file-downloads-csv/ Share on other sites More sharing options...
kickstart Posted August 20, 2010 Share Posted August 20, 2010 Hi If it is data that doesn't need to be secured then just save the file and do a header redirect to the CSV file. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/211276-file-downloads-csv/#findComment-1101623 Share on other sites More sharing options...
Wolphie Posted August 20, 2010 Share Posted August 20, 2010 These are the headers I use when I generate a CSV file. // Set the appropriate headers to tell the browser that it's a file to downlaod header('Content-Type: application/octet-stream'); header('Content-Description: File Transfer'); header('Content-Disposition: attachment; filename="'. $filename .'"'); header('Pragma: no-cache'); header('Expires: 0'); Just simply echo out the data after that and that will be the contents of the CSV file. I can't help you further without viewing your code. Quote Link to comment https://forums.phpfreaks.com/topic/211276-file-downloads-csv/#findComment-1101625 Share on other sites More sharing options...
stuartbates Posted August 20, 2010 Author Share Posted August 20, 2010 Thanks for the replies looking at the second post I see my stupid mistake! For some reason I thought specifiying the filename in this line here: header('Content-Disposition: attachment; filename="'. $filename .'"'); Long and short I wasn't sending any data and simply needed to add: readfile($filename); Rather embarrassing but thanks for the replies guys Quote Link to comment https://forums.phpfreaks.com/topic/211276-file-downloads-csv/#findComment-1101632 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.