Page 1 of 1
need help for file writing
Posted: Wed Jan 18, 2006 11:35 pm
by itsmani1
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.
Posted: Thu Jan 19, 2006 12:27 am
by itsmani1
ok
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);
?>
Posted: Thu Jan 19, 2006 12:36 am
by josh
If you're on php5 it compresses down to
Code: Select all
file_put_contents('abc.html', file_get_contents('http://example.com'));
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)
Re: need help for file writing
Posted: Thu Jan 19, 2006 12:59 am
by Christopher
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?
You might want to look at the output buffering functions. You could do something like this:
Code: Select all
ob_start();
include 'abc.php';
$out = ob_get_contents();
ob_end_clean();
file_put_contents('abc.html', $out);
Posted: Thu Jan 19, 2006 7:29 am
by foobar
itsmani1 wrote:ok
here is the solution that i get and its working:
No it's not. That just retrieves whatever the server spits out.
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 your PHP installation is set to accept
URL fopen wrappers.
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.
Posted: Thu Jan 19, 2006 7:53 am
by matthijs
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?
Browse to abc.php, [view] -> [Page source], [File] -> [Save as]
Or is this too simple?
