Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8631105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:10:17+00:00 2026-06-12T09:10:17+00:00

I create a CSV file for download by our client using $output = fopen(‘php://output’,

  • 0

I create a CSV file for download by our client using

$output = fopen('php://output', 'w');

and using fputcsv() to write data to a CSV file which is downloaded by the client.

I am running PHP on Linux and consequently the line endings are not interpreted by many Windows applications.

I could write the CSV file to a directory on the server, read it back in and perform a str_replace() from \n to \r\n, but this seems a rather clunky way of solving the problem. Is there a way to perform the conversion without creating a physical file?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:10:18+00:00Added an answer on June 12, 2026 at 9:10 am

    You could use stream filters to accomplish this. This example writes to a physical file, but it should work fine for php://output as well.

    // filter class that applies CRLF line endings
    class crlf_filter extends php_user_filter
    {
        function filter($in, $out, &$consumed, $closing)
        {
            while ($bucket = stream_bucket_make_writeable($in)) {
                // make sure the line endings aren't already CRLF
                $bucket->data = preg_replace("/(?<!\r)\n/", "\r\n", $bucket->data);
                $consumed += $bucket->datalen;
                stream_bucket_append($out, $bucket);
            }
            return PSFS_PASS_ON;
        }
    }
    // register the filter
    stream_filter_register('crlf', 'crlf_filter');
    
    $f = fopen('test.csv', 'wt');
    // attach filter to output file
    stream_filter_append($f, 'crlf');
    // start writing
    fputcsv($f, array('1 1', '2 2'));
    fclose($f);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Objective: I need to create a CSV file via PHP but limit download access
I'm using PHP to create a CSV file from a database query. I run
I am using Text::CSV::Slurp to create a CSV file from an array of hashes.
If the task is to create a csv file out of some data where
I want to create a barplot and my data is in a csv file
I have a .csv file containing 3 columns of data. I need to create
I am trying to create a csv file of some data. I have wrote
My goal here is to have the browser download a csv file using headers
I need to create a CSV file from a PHP array, I'm aware of
Possible Duplicate: Create a CSV File for a user in PHP I am looking

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.