Jump to content

File Downloads CSV


stuartbates

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/211276-file-downloads-csv/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/211276-file-downloads-csv/#findComment-1101625
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/211276-file-downloads-csv/#findComment-1101632
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.