file_get_contents - also get the pictures

旧时模样 提交于 2019-12-12 04:01:16

问题


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?


回答1:


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




回答2:


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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/1245452/file-get-contents-also-get-the-pictures

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!