PHP image headers and JavaScript loading

I have a big problem displaying images in fancybox. Images are generated by PHP using header() and readfile() .

When I access the script directly from the browser, everything is fine, but when the request comes from fancybox JavaScript, suddenly the browser stops responding and after a long time in the load content area I get binary instead of image.

Here is the code:

 ob_clean(); header('Content-Type: '.$post->post_mime_type); $name = basename($post->guid); $base = home_url(); $path= $_SERVER{'DOCUMENT_ROOT'}.str_replace($base,'',$post->guid); header ('Content-Disposition: inline; filename='.$name); header ('Content-length: ' .filesize($path)); readfile($path); 

Content screen loaded in fancybox: http://img502.imageshack.us/img502/8535/screenq.png

+4
source share
2 answers

The first 4 bytes of output look like a valid PNG header.

Please make sure $ post-> post_mime_type is "image / png".

I do not know the framework, it may be confused by the unusual header Content-Disposition: inline, delete this line.

+1
source

This is discovered at http://fancybox.net/faq

FancyBox displays a content type from a URL, but sometimes this may not be right. The solution is to force> your type, for example: $ (". Selector"). Fancybox ({'type': 'image'}); (version 1.3+ required)

+1
source

Source: https://habr.com/ru/post/1332604/


All Articles