Web Hosting Talk







View Full Version : how to save an html page using php???


andyxfun
08-22-2009, 08:48 AM
ok,i posted something similar but this is an exact question now:
how do i save an html page code using php?
:)
so let's say i have this page as a template:

<html>
<head>
<title>%title%</title>
</head>
<body>
<p>
Hello, my name is %name% and i am %age% years old!
</p>
</body>
</html>


and i have program.php:

<?php

// load the contents of template file
$html = file_get_contents('my_template.htm');

// search for the following
$search = array('%title%', '%name%', '%age%');

// replace it with the following
$replace = array('Personel Page', 'Latheesan', 21);

// do the actual search & replace
str_replace($search, $replace, $html);

// print the page
echo $html;

?>


now i have a nice html page,how do i save it to the server let's say save/html page/

i'm really noob in php so please help...:)

m_php
08-22-2009, 09:04 AM
You would need to write the html to a file on the server:

http://php.about.com/od/advancedphp/ss/file_write_php.htm

spiralbean
08-22-2009, 12:31 PM
You can simple generate the code dynamically and write it as an html file

You can simply open the file using fopen and write the contents statically

$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Floppy Jalopy\n";
fwrite($fh, $stringData);
$stringData = "Pointy Pinto\n";
fwrite($fh, $stringData);
fclose($fh);

fabin
08-22-2009, 01:55 PM
now i have a nice html page,how do i save it to the server let's say save/html page/

You already have the entire HTML text in $html. Just write it to a file using file_put_contents ( http://us.php.net/manual/en/function.file-put-contents.php ). Just a single line of code ;)