<?php
// setup stylesheet modification process
if ($_POST['submit'] == "update file") {
$filename = "./style.css"; /* file to write to could be changed to a variable passed by the form */
$copyname = "./style.copy.css"; /* using this makes a backup copy again this could be changed to a variable and have .bak appended to the filename */
// open the style.css file to make a copy of the file
$css_r = fopen($filename, "r");
$style_copy = fread($css_r, filesize($filename));
fclose($css_r);
// as changes have been made lets make a backup
$css_copy = fopen($copyname, "w");
$fcopy_out = fwrite($css_copy, $style_copy);
fclose($css_copy);
// copy is made now lets update the style.css file
$css_w = fopen($filename, "w");
$fstyle_out = fwrite($css_w, $_POST['contents']);
fclose($css_w);
// check if there were any problems writing to files.
if ($fstyle_out != strlen($_POST['contents']) || $fcopy_out != strlen($style_copy)) {
die ("Error writing to file\nPlease make sure the directory\nis CHMOD 777");
} else {
header ("Location: file_written.html"); /* this just redirects you to a new page which says the file has been written to */
}
} else {
// open the style.css file and get the contents for the form
$filename = "./style.css"; /* You could change this to modify a different file or you can get the $filename variable from another form or something */
$fp = fopen($filename, "r");
$contents = fread($fp, filesize($filename));
fclose($fp);
// setup html vars
$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?".">";
$html = <<< EOF
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Yoric's Place</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
input {
border: 1px solid #000000;
}
textarea {
border: 1px solid #000000;
}
-->
</style>
</head>
<body>
<h2>File Modifier</h2>
<form action="{$_SERVER['PHP_SELF']}" method="post">
<p><br />
<textarea name="contents" cols="40" rows="20" id="contents" wrap="none">{$contents}</textarea>
<br />
<input name="submit" type="submit" id="submit" value="update file" />
</p>
</form>
</body>
</html>
EOF;
echo $xml;
echo $html;
}
?>