PHP Image content type problem

10,931

Solution 1

Possibly the image doesn't fit into memory. Or your PHP installation doesn't have permissions to make external HTTP calls. Anyway, I suggest you never use echo file_get_contents(), use readfile instead. Also you should never use raw strings from $_GET or $_POST for file operations. Always strip null-bytes, slashes and double dots from user-provided filenames, or better yet, allow only alphanumeric characters.

Solution 2

I was doing something like this recently, but found this a slow method (I was doing 15+ on a page). This is slow because first your server has to download the image, and then send it to the client. This means for every image it is downloaded twice.

I came up with an alternative - redirection. This allowed the client machines to directly access the other site while hiding the real url in the HTML source code.

$r - is processed above the script, and validated to make sure it is ok.

$webFile = 'http://www.somesite.com/'.$r['type'].'/'.$r['productid'].'.jpg';
header('Location: '.$webFile);
exit();

Granted if someone put my image url in the address bar, it would redirect and the user would see the real url, but it made my page faster and I wasn't too worried about that.

Solution 3

You'll want to ensure that your script is not outputting any white-space. Check before and after the opening/closing PHP tags.

If that checks out, you'll want to ensure that allow_url_fopen is set to On in php.ini

Share:
10,931
Mirko
Author by

Mirko

Updated on November 20, 2022

Comments

  • Mirko
    Mirko over 1 year

    I have a specific problem, and cant get over it.

    For my latest project I need a simple PHP script that display an image according to its ID sent through URL. Here's the code:

    header("Content-type: image/jpeg");
    $img = $_GET["img"];
    echo  file_get_contents("http://www.somesite.hr/images/$img");
    

    The problem is that the image doesn't show although the browser recognizes it (i can see it in the page title), instead I get the image URL printed out.

    It doesn't work neither on a server with remote access allowed nor with one without. Also, nothing is printed or echoed before the header.

    I wonder if it is a content type error, or something else. Thanks in advance.

    • zod
      zod over 13 years
      you want to show the image on browser or download the image?
    • ahmet alp balkan
      ahmet alp balkan over 13 years
      make sure that echo works. try to fetch that page with curl or wget. and make sure that no preceeding whitespace exists before echoing.
    • Mirko
      Mirko over 13 years
      I resolved the first two problems, it really was a whitespace and a code error, but I still got the remote access issue, so I'm forced to use CURL. Can u help me with that, cos I'm not so good with CURL?
  • Mirko
    Mirko over 13 years
    The image is very small and this is just a testing script. I agree with you in using readfile and for the other tips, but i already tried readfile and doesn't work neither.
  • Mirko
    Mirko over 13 years
    I managed to get here, but in a strange way when i send the image ID it doesn't work, it doesn't display the image. Instead when I click on the image source I get the message "The document has moved".