收录日期:2020/06/02 02:00:37 时间:2009-08-07 15:35:26 标签:php,image,file-get-contents

Hello there everybody! I've run into a problem lately when coding in PHP and file_get_contents. My problem is that when i load a website like this:

<? echo file_get_contents($_GET['url']); ?>

The pictures of the website i load doesn't show. For example when I go to Google, no pictures are shown. This is for every website i visit. How can i fix this?

The HTML page you are displaying assumes you also have the images available, which you don't as they are on the original page's server (e.g. Google.com).

The quickest way to make sure everything on the HTML page loads is to add <base href="http://www.google.com/" />. This tells the browser to go back to the original path for the rest of the contents including images, CSS, scripts etc.

You'll want to inject that between the <head></head> of the HTML page you're displaying. You could use a regular expression or Simple HTML DOM.

Hope that helps

Don't do this. You're stealing other web sites' content. It also doesn't work well, as you've noticed, since all relative URLs are broken.

Can you use an iframe instead? As in:

<iframe src="<?php echo htmlspecialchars($_GET['url']) ?>"></iframe>

This is nicer since you're not hiding the web site you're proxying from the end user.

I think this is because the image urls are relative <img src="/img/foo.png"> meaning it looks for the image on your server instead and say googles. Fixing this requires looking through all urls in the source and changing them from relative to absolute.

file_get_contents() does what it says, gets the content of the file or Url supplied as argument. An HTML page doesn't have images inside it, they're not the page's content, the HTML page only has references to external files, that have their own content.