1
<?php
  ob_clean();
  header('Content-Type: image/jpeg');
  header('Content-Length: ' . filesize($file));
  $file = '111111-11111.jpg';
  $handle = fopen($file, "r");
  echo file_get_contents($file);
?>

At one point my php webpage stopped displaying all and any images I had successfully uploaded to MySQL database. I wanted to get down to the roots of this problem and see if it could output a .jpg image stored on HDD and all I get is the same output as when I'm trying access this image from database:

The image [...] could not be displayed because it contains errors.

The image location is correct, there are no whitespaces before header, the output buffer has been cleaned. I have spent days on google searching for the answer, but without luck.

I am quite new to PHP so if you can help, please be as descriptive as you can be, so I could understand. Thank you in advance.

3
  • 1
    Have you checked the error_log for any PHP errors, perhaps permission based? Also, you don't need the $handle = ... line as you are not using it Commented Feb 6, 2014 at 9:00
  • Check the raw output from the page. It might have some errors in the stream. Use ini_set('display_errors', 0); to disable that. Commented Feb 6, 2014 at 9:02
  • Also, you don't need to fopen() the file before using file_get_contents(). Commented Feb 6, 2014 at 9:03

1 Answer 1

1

Be sure that the file you are trying to output is in the correct path (from the code, the same as the PHP script) and that you have the read permissions on the file.

$file = '111111-11111.jpg';
$type = 'image/jpeg';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($file));
readfile($file);
Sign up to request clarification or add additional context in comments.

5 Comments

To be noted: The order of instructions was to be changed and this one looks better (filesize calculation was being done before setting the path to the file)
Tried your suggested code, and it's still giving me the same error. The file really is there, under the same name and I DO have the necessary permissions, but It is still not giving me anything.
The image [...] could not be displayed because it contains errors.<<< this error, if you can call it an error. Everything I do leads back to this.
Try another image and let me know.
@Napolux Okay, so maybe I am dumb... Just tried to open windows' sample .jpg picture, and it worked. That leaves me with a question - why is it giving me error when this picture is shown once I put it on a database?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.