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 8457753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:53:28+00:00 2026-06-10T12:53:28+00:00

The snippet reads an ouput stream in a new file CSV. That part works.

  • 0

The snippet reads an ouput stream in a new file CSV. That part works. I can open the file from the server and it looks perfect. The problem is with the file which downloads to my hard drive through the browser. It will not open and read properly in spreadsheet software or Excel on a Windows machine:

$NewFile = fopen($FileName,"w");
fwrite($NewFile, $output);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$FileName.'"'); 
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($NewFile));
ob_clean();
flush();
readfile($NewFile);

The content of the CSV looks ��ࡱ� like when I open it from the browser download but looks perfect when I open it directly on the server or download the stored file with FTP.

  • 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-10T12:53:30+00:00Added an answer on June 10, 2026 at 12:53 pm

    The browser sees the application/octet-stream as a binary type. You need a text/plain content type:

    header('Content-Type: text/plain');
    // Or:
    header('Content-Type: text/csv');
    // Or: 
    header('Content-Type: application/csv');
    

    The Content-Transfer-Encoding header should be unnecessary if you have the Content-Type correctly set, and indeed it is probably misleading the browser into thinking it has received a binary file as well:

    // No need, possibly harmful. Remove it...
    // header('Content-Transfer-Encoding: binary');
    

    ###Update:

    I see another problem. You are setting the Content-Length not to the size of the file, but rather to the file handle opened by fopen(), which mis-informs the browser of the number of bytes to expect. filesize() takes a string filename as its argument, not a file handle. You will probably need to close the handle with fclose($NewFile) before calling filesize().

    // Instead of:
    header('Content-Length: ' . filesize($NewFile));
    
    // You mean to use $FileName
    // close the file handle first...
    fclose($NewFile);
    header('Content-Length: ' . filesize($FileName));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a snippet of code that reads a text file with 10
Snippet from a SQL query (using SQL Server 2008 R2): , CONVERT (VARCHAR(10), s.ENTER_DATE,
snippet from The Server code : public void run() { try { // Create
I am making a prototype client & server so that I can understand how
I'm getting a html snippet from json that I'm bringing into my template, but
I am writing code for uploading a file from a client to my server
I'm trying to convert a .db file to binary so I can stream it
I'm creating a wrapper for a Minecraft Server that is supposed to take new
Below is a snippet from a larger script that exports a list of the
Following is a part of the code snippet that I will be using for

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.