Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Apr 2009
    Posts
    135
    Thanks
    83
    Thanked 0 Times in 0 Posts

    How To Display An Image Using PHP From A URL?

    I want to display an image from a URL when a file is browsed to directly.

    I have tried file_get_contents. CURL to get contents, include(""); and all of them just display strange code like:
     ě Ó�JFIF��H�H�� ■›┴�������������

    Apart from include(""); which failed to open the file.

    Is there any way to display an image without having a page source code?

  • #2
    Regular Coder Iszak's Avatar
    Join Date
    Jun 2007
    Location
    Perth, Western Australia
    Posts
    332
    Thanks
    2
    Thanked 58 Times in 57 Posts
    It's because you haven't set any content type via the header header so it displays it as text, if you were to set it like.
    PHP Code:
    header('Content-type: image/png');

    // get the file contents 
    You should be able to display it, of course the content type would vary from what you're displaying.

  • #3
    Regular Coder
    Join Date
    Apr 2009
    Posts
    135
    Thanks
    83
    Thanked 0 Times in 0 Posts
    Here is my code that is not working:
    PHP Code:
    <?php
    header
    ('Content-type: image/jpeg;');
    $p "http://icanhascheezburger.files.wordpress.com/2008/01/funny-pictures-cute-fierce-kitten.jpg";
    $a file_get_contents('$p');
    echo 
    $a;
    ?>
    I have added a header and tried this with more than one image but it will not seem to load the URL for me, have I got something wrong here? :S

  • #4
    Regular Coder Iszak's Avatar
    Join Date
    Jun 2007
    Location
    Perth, Western Australia
    Posts
    332
    Thanks
    2
    Thanked 58 Times in 57 Posts
    Well besides having file_get_contents wrapped in single quotes, it works perfectly for me.

  • Users who have thanked Iszak for this post:

    Sussex_Chris (06-12-2009)

  • #5
    Regular Coder
    Join Date
    Apr 2009
    Posts
    135
    Thanks
    83
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Iszak View Post
    Well besides having file_get_contents wrapped in single quotes, it works perfectly for me.
    That was my problem, thanks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •