Save current php page content into a html file

Discuss coding issues, and scripts related to PHP and MySQL.
PloMar
Posts: 48

Save current php page content into a html file

Hello,
How can I save the content of the current php page into a html file, only after a form is submitted?
For example, I have the following code into a php file which displays some data and a form in the web page.
Now, when I submit the form (to the same php file) I want to save the whole page content into a html file on server (including data added in the form).
This is the code:

Code: Select all

<?php
$dir = 'dir/';  // where to save
$file_html = 'file.html';  // name of the html file

// Variable with data from forrm, or default 'site'
$site = isset($_POST['site']) ? $_POST['site'] : 'site';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Title</title>
<meta charset="utf-8">
</head>
<body>
<?php
// output the value of $site
echo $site;
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
 <input type="text" name="site" value="https://coursesweb.net/" />
 <input type="submit" name="save" value="Send" />
</form>
</body>
</html>

Admin Posts: 805
Hello,
Add the ob_start() before the content you want to display in the web page, to store that content in memory. Then, when the content is finished, use ob_get_clean() to get it into a variable.
To save the variable data, apply file_put_contents() when form data is submitted.
See this code:

Code: Select all

<?php
$dir = 'dir/';  // where to save
$file_html = 'file.html';  // name of the html file

// Variable with data from forrm, or default 'site'
$site = isset($_POST['site']) ? $_POST['site'] : 'site';

// to keep in memory the output data
ob_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Title</title>
<meta charset="utf-8">
</head>
<body>
<?php
// output the value of $site
echo $site;
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
 <input type="text" name="site" value="https://coursesweb.net/" />
 <input type="submit" name="save" value="Send" />
</form>
</body>
</html>
<?php
// get output data stored in memory
$content = ob_get_clean();

// salve the page content (return message in case of error)
if(isset($_POST['save'])) {
  if(!file_put_contents($dir . $file_html, $content)) echo 'Unable to save: '. $dir . $file_html;
}

echo $content;  // output the page content
?>

MarPlo Posts: 186
If you want to save the page content after a form is submitted, another way is to add the page code into a hidden field in that form, with JavaScript before submitting. Then, in php just get data from that field and save it with file_put_contents(). So, you not need of ob_start() /ob_get_clean().
Here's an example:

Code: Select all

<?php
$dir = 'dir/';  // where to save
$file_html = 'file.html';  // name of the html file

// Variable with data from form
$site = isset($_POST['site']) ? $_POST['site'] : 'site';
$page_cnt = isset($_POST['page_cnt']) ? html_entity_decode(trim($_POST['page_cnt'])) : '';

// salve the $page_cnt data (return message in case of error)
if(isset($_POST['page_cnt'])) {
  if(!file_put_contents($dir . $file_html, $page_cnt)) echo 'Unable to save: '. $dir . $file_html;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Title</title>
<meta charset="utf-8">
</head>
<body>
<?php
// output the value of $site
echo $site;
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="frm1">
 <input type="hidden" name="page_cnt" id="page_cnt" value="" />
 <input type="text" name="site" value="https://coursesweb.net/" />
 <input type="submit" name="save" value="Send" />
</form>
<script>
// when submitting the form, add all the page content in #page_cnt
document.getElementById('frm1').addEventListener('submit', function(){
  // replace & < > " with their html entities to can be added in form field
  document.getElementById('page_cnt').value = document.querySelector('html').outerHTML.replace(/&/g, '&').replace(/\</g, '<').replace(/\>/g, '>').replace(/"/g, '"');
});
</script>
</body>
</html>

Similar Topics