File_get_contents does not display image correctly

I am trying to display shared sites and URLs as part of my own site. Here is part of the relatively simple code that I use:

browse.php

<?   
$enteredurl = $_GET["url"];
$page = file_get_contents($enteredurl);
echo $page;
?>

Ignoring the fact that some links / images will not work if the URls are relative and not absolute, this works fine. Access to the pages is done using $ _GET, something like

browse.php? url = http://itracki.com

The web page will display as expected. However, when I try to get something else, like an image, do I get something like this, which I think is binary or something else?

browse.php? url = http://images.itracki.com/2011/06/favicon.png

‰PNG  IHDRóÿa%IDAT8Ëc8sæÌJ0M ```ã3`xaÔ€aa]r#f.–ÄíNIEND®B`‚

, -

<? header('Content-type: image/png;'); ?>

. - , , , " http://images.itracki.com/2011/06/favicon.png"?

!

+2
3

:

<?php
  $img = 'http://images.itracki.com/2011/06/favicon.png';
  $info = getimagesize($img);
  header('Content-type: ' . $info['mime']);
  readfile($img);
?>

readfile() file_get_contents() , readfile() , , file_get_contents().

, content-type , .

, content-type mime-type, .

+6

$image = 'http://images.itracki.com/2011/06/favicon.png';
// Read image path, convert to base64 encoding
$imageData = base64_encode(file_get_contents($image));

// Format the image SRC:  data:{mime};base64,{data};
$src = 'data: '.mime_content_type($image).';base64,'.$imageData;

// Echo out a sample image
echo '<img src="',$src,'">';
+3
echo 'data:;base64,'.base64_encode(file_get_contents('http://images.itracki.com/2011/06/favicon.png'));
-2
source

All Articles