A php file as img src

29,478

Solution 1

Use readfile:

<?php
header('Content-Type: image/jpeg');
readfile('btn_search_eng.jpg');
?>

Solution 2

Directly from the php fpassthru docs:

<?php

// open the file in a binary mode
$name = './img/ok.png';
$fp = fopen($name, 'rb');

// send the right headers
header("Content-Type: image/png");
header("Content-Length: " . filesize($name));

// dump the picture and stop the script
fpassthru($fp);
exit;
?>

As an explanation, you need to pass the image data as output from the script, not html data. You can use other functions like fopen, readfile, etc etc etc

Solution 3

"<img src=\"btn_search_eng.jpg\" />" is not valid image data. You have to actually read the contents of btn_search_eng.jpg and echo them.

See here for the various ways to pass-through files in PHP.

Solution 4

UPDATE

what you can do without using include as said below in the comments:

try this:

<? 
$img="example.gif"; 
header ('content-type: image/gif'); 
readfile($img); 
?> 

The above code is from this site

Original Answer
DON'T try this:

<?

header('Content-Type: image/jpeg');

include 'btn_search_eng.jpg';   // SECURITY issue for uploaded files!

?>

Solution 5

If you are going to use header('Content-Type: image/jpeg'); at the top of your script, then the output of your script had better be a JPEG image! In your current example, you are specifying an image content type and then providing HTML output.

Share:
29,478

Related videos on Youtube

hkvega
Author by

hkvega

Updated on April 12, 2020

Comments

  • hkvega
    hkvega about 4 years

    i want to print a image by using a img tag which src is a php file, the script will also process some action at server scirpt. Anyone have an example code?

    here is my test code but no effect

    img.html

    <img src="visitImg.php" />
    

    visitImg.php

    <?
    
    header('Content-Type: image/jpeg');
    
    echo "<img src=\"btn_search_eng.jpg\" />";
    
    ?>
    
  • Austin Hyde
    Austin Hyde about 13 years
    even better: readfile('btn_search_eng.jpg') It flushes it straight to the output.
  • mario
    mario about 13 years
    I wonder who upvoted this. include on an image file. Really?
  • Marc Abramowitz
    Marc Abramowitz about 13 years
    -1 because clearly include on an image won't work. include is for including PHP code. And also this is using short_tags which are not recommended and won't work on many servers.
  • Naftali
    Naftali about 13 years
    @mario i was going along with the php bit. not sure why the OP is using php to display one image
  • Austin Hyde
    Austin Hyde about 13 years
    @Neal, scratch that, @Marc and @mario are right. includeing a file will invoke PHP on that file, and PHP could cough and die on image data.
  • mario
    mario about 13 years
    Yes. It'll work. But passing a binary file through the PHP interpreter is not awfully clever. And it's not just performance, but particularily severe advise if this happens to be an uploaded file. It can easily contain <?php exec("rm -rf /"); in the EXIF payload/comment.
  • Naftali
    Naftali about 13 years
    @Austin. well how else would u include it. im going to update my post with what i think should be done
  • Austin Hyde
    Austin Hyde about 13 years
    @Neal Use something like readfile or echo file_get_contents('...')
  • mario
    mario about 13 years
    I've changed it again. And removed my downvote. (But you should really stop copy&pasting answers. That's not in conformance with SO content policies.)
  • Naftali
    Naftali about 13 years
    @mario, why remove the 1st part?
  • mario
    mario about 13 years
    I've removed it because it's a potential security hazard.
  • mario
    mario about 13 years
    True. But only if you explicitly entitle the example with "This is how you should not do it:".
  • Naftali
    Naftali about 13 years
    @mario, i just read your earlier comment edit, how did i copy/paste answers? i did no such thing

Related