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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:28:19+00:00 2026-06-13T23:28:19+00:00

I currently have the following: if (headers_sent()) { echo ‘HTTP header already sent’; }

  • 0

I currently have the following:

if (headers_sent()) {

    echo 'HTTP header already sent';
} else {
    if (!is_file($path)) {
        header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');
        echo 'File not found';
    } else if (!is_readable($path)) {
        header($_SERVER['SERVER_PROTOCOL'].' 403 Forbidden');
        echo 'File not readable';
    } else {
        ob_start();
        header($_SERVER['SERVER_PROTOCOL'].' 200 OK');
        header('Content-Description: File Transfer');
        header("Content-Type: octet-stream");
        header("Content-Transfer-Encoding: binary");
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header("Content-Length: ".filesize($path));
        header("Content-Disposition: attachment; filename=\"".basename($path)."\"");
        ob_clean();
        ob_flush();
        readfile($path);

        exit;

The above code produces a file download of the correct file name, but with a twist.

The downloaded file has some html from the download page (headers being sent incorrectly?) then later in the downloaded document there is gibberish text (ex. OJQJ^J.??. WW8Num2z0) taking up several pages. Finally at the end of the .doc I can read some English which appears to be the correct text of the .doc, but with no formatting.

These issues persist in Chrome and Mozilla.

Any help would be greatly appreciated. Thanks in advance.

EDIT:

I began testing with a .doc that has only one line of text: “test”

The file can be opened manually from the directory in which it is uploaded to, and verified that it is not corrupt. However, downloading this file using any of the methods recommended thus far yields several pages of gibberish text (the real file is only 1 line). The actual text of the file, which is “test”, is found on the very last of the 66 pages of gibberish, and it is found in this haystack: ?test ?”ᄚ? ᄚ?!ᄚn”ᄚn#ミn$ミn3P(20???՜.モラ+,??՜.モラ+,???Root Entry???????? ?F@CompObj????jOle ????????1Table????????????ᆬSummaryInformation(?????WordDocument????????????”$DocumentSummaryInformation8????????????[t????????????????

  • 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-13T23:28:20+00:00Added an answer on June 13, 2026 at 11:28 pm

    What are these?

        header("Content-Type: octet-stream");
        header("Content-Transfer-Encoding: binary");
    

    Trim down your code to a working example and extend it from there. You do not need to set the status code, PHP does this for you. You do not need to set the Content-length either. Sending only this should just work:

    $path = 'foo.dat';
    
    if (headers_sent()) {
        echo 'HTTP header already sent';
    } else {
        if (!is_file($path)) {
            header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');
            echo 'File not found';
        } else if (!is_readable($path)) {
            header($_SERVER['SERVER_PROTOCOL'].' 403 Forbidden');
            echo 'File not readable';
        } else {
            header("Content-Disposition: attachment; filename=\"".basename($path)."\"");
            readfile($path);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently I have following XML: <Rowsets> <Rowset> <Row> <DrilldownDepth>0</DrilldownDepth> <ScheduleWeek>---</ScheduleWeek> <ABC>---</ABC> <PQR>1500</PQR> <XYZ>15000</XYZ> <Quant>285</Quant>
I currently have the following controller method in a Rails app: def index @entries
I currently have the following SQL query that lists all the names of all
I currently have the following Get-ChildItem -recurse -include file.ext | Select-String -pattern c: |
I currently have the following htaccess rewrite rule: RewriteRule (.*) index.php/$1 [L] How do
I currently have the following CSS (called splash.css): html { background:url(splash.jpg) center no-repeat; background-size:auto
I currently have the following code which delays showing a fixed bar after a
I currently have the following in select expert to select a bunch of fields:
I currently have the following CSS for my 4 columns which might grow to
I currently have the following row in my table: course_data: user_id days <-- This

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.