requiem31 Posted June 4, 2014 Share Posted June 4, 2014 I am having trouble with displaying an image from a url. In the following code if I echo out $test I get the url of the image. However, in the current code I am trying to display the image, but the only thing that gets displayed is a small broken image in the upper left. $html = file_get_contents($website.$criteria); $dom = new DOMDocument; @$dom->loadHTML($html); $links = $dom->getElementsByTagName('img'); header('Content-Type: image/png'); foreach ($links as $link){ $test = $link->getAttribute('src'); echo file_get_contents($test); } image that gets displayed: http://imgur.com/epfF3wJ Quote Link to comment https://forums.phpfreaks.com/topic/288989-trouble-with-displaying-an-image-from-a-url/ Share on other sites More sharing options...
gizmola Posted June 4, 2014 Share Posted June 4, 2014 A url is not the same thing as the path to a file. Based on what you're trying to do here, you need the filesystem path to the images. Quote Link to comment https://forums.phpfreaks.com/topic/288989-trouble-with-displaying-an-image-from-a-url/#findComment-1481902 Share on other sites More sharing options...
requiem31 Posted June 4, 2014 Author Share Posted June 4, 2014 But if I copy and paste the url into a browser it displays the image Quote Link to comment https://forums.phpfreaks.com/topic/288989-trouble-with-displaying-an-image-from-a-url/#findComment-1481903 Share on other sites More sharing options...
gizmola Posted June 4, 2014 Share Posted June 4, 2014 Yes, which is how browsers work --- they make HTTP requests from the client to the server, and the image resource is assembled on the client. You are coding a PHP/Serverside script. Are these images on your server? If so, your script should access the image files directly. Strictly speaking file_get_contents can use the HTTP wrappers if that is turned on, but as it's a huge security hole, most servers do not have that feature enabled for these types of file oriented functions. Quote Link to comment https://forums.phpfreaks.com/topic/288989-trouble-with-displaying-an-image-from-a-url/#findComment-1481904 Share on other sites More sharing options...
requiem31 Posted June 5, 2014 Author Share Posted June 5, 2014 oh ya its not on my server. Is there any way to do that? Quote Link to comment https://forums.phpfreaks.com/topic/288989-trouble-with-displaying-an-image-from-a-url/#findComment-1481905 Share on other sites More sharing options...
mogosselin Posted June 5, 2014 Share Posted June 5, 2014 If it's not on your server, make sure that you have the rights to do what you want to do. The question: What do you want to do with those images? Display all of them on one HTML page? Download it on your server? Some kind of proxy that you can call on your server and it will display an image from a distant website? But, if you just want to display all the images found in the distant HTML page: Remove the line header('Content-Type: image/png'); In your "foreach", instead of echo file_get_contents($test); try: echo "<img src='$test' />"; Now, you'll have an HTML page that displays the images. Only your HTML page will miss the <HTML>, <HEAD>, <BODY> etc.. tags. But it should work if you only want to display the images for yourself. If it's for a public website, ensure that you have a valid HTML page. Quote Link to comment https://forums.phpfreaks.com/topic/288989-trouble-with-displaying-an-image-from-a-url/#findComment-1481907 Share on other sites More sharing options...
gizmola Posted June 5, 2014 Share Posted June 5, 2014 oh ya its not on my server. Is there any way to do that? Yes, access the files directly using the full filesystem path to them. I've stated this 3x now. Quote Link to comment https://forums.phpfreaks.com/topic/288989-trouble-with-displaying-an-image-from-a-url/#findComment-1481953 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.