How can i get html code of a page using php? for example there is a page abc.php, i want to save this page's html code on my web server as abc.html, how can i do that?
thanx.
need help for file writing
Moderator: General Moderators
- itsmani1
- Forum Regular
- Posts: 791
- Joined: Mon Sep 29, 2003 2:26 am
- Location: Islamabad Pakistan
- Contact:
ok
here is the solution that i get and its working:
here is the solution that i get and its working:
Code: Select all
<?php
$contents = fopen("http://www.msn.com/","r");
$outputfile = fopen ("ciao.html","w");
while (!feof($contents)) {
$read = fread($contents,9999);
fputs($outputfile,$read);
}
fclose($outputfile);
fclose($contents);
?>If you're on php5 it compresses down to
also don't do this unless you can gaurantee the contents of that remote file are not something dangerous (especially if you have PHP set to parse .html files) let's say the owner of the site you're grabbing the html from gets <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span> and decides to put some javascript on the page that you were grabbing which then makes it through to your visitors.. this is called XSS (cross site scripting)
Code: Select all
file_put_contents('abc.html', file_get_contents('http://example.com'));- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: need help for file writing
You might want to look at the output buffering functions. You could do something like this:itsmani1 wrote:How can i get html code of a page using php? for example there is a page abc.php, i want to save this page's html code on my web server as abc.html, how can i do that?
Code: Select all
ob_start();
include 'abc.php';
$out = ob_get_contents();
ob_end_clean();
file_put_contents('abc.html', $out);(#10850)
No it's not. That just retrieves whatever the server spits out.itsmani1 wrote:ok
here is the solution that i get and its working:
If files can be remotely included, then using the following syntax should work:
Code: Select all
include('http://example.com/path/to/script.php');If you just want to rip other people's source-code, then you're outta luck and on the safe side of the law as well.