0

I'm trying to show any file in the browser using PHP and headers, but it isn't working with a PNG image, it shows a strange black square like this:

The Strange Blank Square

Here is my code:

<?php
    header('Content-Type: ' . mime_content_type($filepath));
    readfile($filepath);
    die();
?>

I already checked the $filepath variable, its the full path (E:\wwwPublic\myapp\application\controllers\..\..\documents\34807\image.PNG) I don't know what is going wrong in that code.

Do you have any idea(s) ?

  • The strange black squares says that you are reading an image file. Aren't you? – Praveen Kumar Purushothaman Jun 13 '17 at 13:10
  • Yes, I think this is what it means, but the blank square isn't the image I'm trying to read – Alex'Presso Jun 13 '17 at 13:12
  • is the header set before anything is sent to display to the page? (not even a doctype or html tag, nothing must be displayed to document at header time) – Kaddath Jun 13 '17 at 13:12
  • No, the source code only display the image data – Alex'Presso Jun 13 '17 at 13:14
  • if this is the only code in your php file, then `$filepath` is not defined. If this is included in another file, be careful that the file that inludes this must not have any display output before the include. Anyway, what do you get when you display `mime_content_type($filepath)` value? – Kaddath Jun 13 '17 at 13:20
  • you need to use the right header type. – Funk Forty Niner Jun 13 '17 at 13:30
  • Yes, I'm including it from Code Igniter, but there is nothing displayed on page before I call this, the `mime_content_type()` contains `image/png` – Alex'Presso Jun 13 '17 at 13:35
  • mmmh don't know then.. do you get the image when you call directly this file (with a static filename string, or $filename var assignment before the header)? – Kaddath Jun 13 '17 at 13:52

1 Answers1

0

I solved it, I found that Code Igniter has default headers, I needed to add ob_clean() to remove all of them and add my own, Thank you for your help