need help for file writing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

need help for file writing

Post 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.
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post 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);
?>
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: need help for file writing

Post 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);
(#10850)
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post 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.
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post 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? :)
Post Reply