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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:13:21+00:00 2026-05-25T13:13:21+00:00

A few months ago I wrote a website for a customer using PHP 5.3.

  • 0

A few months ago I wrote a website for a customer using PHP 5.3. It works perfectly on my own LAMP webserver. However, when he tried to install it in his own server (currently an OVH server running DirectAdmin on CentOS 5) he ran into an issue I’m having trouble figuring out.

The website can store images uploaded through forms. Images are watermarked on upload and moved to a directory in the webserver (some metadata is stored in the database, but this isn’t related to this issue).

In order to display these images back to the user, a script is used like so:

header("Content-type: image/jpeg");
ob_start();
echo file_get_contents($path);
$size = ob_get_length();
$img = ob_get_contents();
ob_end_clean();
header ("Content-length: " . $size);
echo $img;

Unfortunately, this always returns a broken image (in Firefox, “The image cannot be displayed because it contains errors”). Now, after careful testing, I know that:

  • Images are correctly uploaded to the server. The image data stored in the webserver is valid and can be obtained via FTP as the regular image.

  • If I store $img to a file immediately before the final line of the previous script, like so:

    $fh = fopen("test.jpg", "w");
    fwrite($fh, $img);
    fclose($fh);
    

    It will save the correct image data to the file also. So the data is intact immediately before being sent to the user’s web browser.

  • The headers are being sent correctly.

However! If I use a text/plain header rather than image/jpeg, I can see that the gibberish returned is different from the gibberish displayed if I open the file locally with notepad (or send the image directly through apache as a text file). In the original image, I can see some EXIF. In the image generated by PHP and then send to the user’s web browser, I still see the JFIF magic code (for JPEG File Image Format) but the rest looks different.

I’m afraid I have a configuration-related issue on either PHP or Apache related to encoding, buffering, content compression or something like that. Does anyone know anything I can try to solve this issue?

EDIT:

Changed the script to use:

$img = file_get_contents($path);
$size = filesize($path);

The issue remains unchanged, but the content now looks exactly the same comparing the real image to the image sent from PHP. According to the headers, the content encoding is gzip. Any ideas?

  • 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-05-25T13:13:21+00:00Added an answer on May 25, 2026 at 1:13 pm

    Well, after some investigation it turned to be infamous Byte Order Mark signature (in conjunction, of course, with output buffering which suppressed an error) in PHP script.
    It seems just re-saving the file without BOM will solve the problem

    does it work?

    header("Content-type: image/jpeg");
    echo file_get_contents($path);
    

    or this?

    header("Content-type: image/jpeg");
    readfile($path);
    

    download this image (using wget or make a link on it and use ‘Save as’) and see the difference. It may shed the light on the cause

    And yeah. ob has absolutely nothing to do here. if you want to get a file size – there is a (surprise!) a function for it

    header("Content-type: image/jpeg");
    header ("Content-length: " . filesize($path));
    readfile($path);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From few months ago when i was using twitter, i was able to send
A few months ago, I was working on a PHP project in Aptana Studio.
I used a tool a few months ago that scanned a specified website and
I've starting using Codeigniter for a project recently (few months ago) but its getting
I have a website that was launched a few months ago. I would like
I downloaded and installed Eclipse using Yoxos a few months ago. Since then, several
Few months ago i made an app based on apple GLPaint. It works great.
A few months ago i asked the question below. My question. However, i have
I started using primefaces a few months ago, and I couldn'd find out a
I only got into PHP a few months ago and have been dabbing into

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.