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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.