Criss
Criss

Reputation: 983

PHP cannot output images

<?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.

Upvotes: 1

Views: 165

Answers (1)

napolux
napolux

Reputation: 16084

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);

Upvotes: 1

Related Questions