Display image from url

Hello, I wanted to actually insert image into mysql blob field by getting the image from url. But I can’t even display the image into the browser from the url.

http://localhost/sks/test.php


<?php
header('Content-type: image/jpeg');
echo file_get_contents("http://icanhascheezburger.files.wordpress.com/2008/01/funny-pictures-cute-fierce-kitten.jpg");
?>

result is


http://localhost/sks/test.php

it shows the url instead of the image, but that url has become an image type because I cannot select the text.

Why is it so ? Thanks

This code works for me:


<?php
header('Content-type: image/jpeg');
echo file_get_contents("http://icanhascheezburger.files.wordpress.com/2008/01/funny-pictures-cute-fierce-kitten.jpg");
?>

Make sure you DO NOT have anything else in your PHP file before or after the <? and ?>

I had a similar problem once and I am trying to remember how I ended up solving it.

If my memory serves me correctly, I believe i solved that problem because it ahd something to do wil relative and absolute URLs at PHP

the file whose contents you echo is being called o by a absolute physical URL… so i dont see why the server shouldn’t be returning the image?

maybe icanhazcheezburger does not giver users or other servers access to the files

I wish i could help more but I ameager to learn the answer!

Best of luck & Regards,
Team 1504

I tried your code, it’s not working too, it shows that url again. I had made sure there is no space or anything before and after the php tag. I have tried other browsers(IE, chrome) both of them didn’t show the URL like firefox did, but show an icon of unavailable image.

Could it be to do with my php configuration ?

But based on http://my.php.net/file_get_contents manual,
file_get_contents — Reads entire file into a string

It just return string ? so image wouldn’t work ? But I have seen other people who used this function to display image too.

Thanks

Works just fine here.


<?php
if(false !== ($data = file_get_contents('http://icanhascheezburger.files.wordpress.com/2008/01/funny-pictures-cute-fierce-kitten.jpg'))){
  header('Content-type: image/jpeg');
  echo $data;
}
?>


<?php
if(true === function_exists('curl_init')){
  $ch = curl_init('http://icanhascheezburger.files.wordpress.com/2008/01/funny-pictures-cute-fierce-kitten.jpg');
  curl_setopt_array(
    $ch,
    array(
      CURLOPT_RETURNTRANSFER  => true
    )
  );
  $data = curl_exec($ch);
  curl_close($ch);
  header('Content-type: image/jpeg');
  echo $data;
}
?>

Great this method work for me, but the file_get_contents doesn’t work. I wonder why is it so ? But thanks a lot.

Check out the allow_url_fopen directive. :wink: