File Content:
asnasd as
asnasd as
<?php
include('includes/header.php');
// define the file path
$file_path = 'doc/text.txt';
// writing to the file
if (isset($_POST['content'])) {
$content_to_write = $_POST['content'];
// open the file in write mode ('w' overwrites the file)
$file_handle = fopen($file_path, 'w');
if ($file_handle) {
fwrite($file_handle, $content_to_write);
fclose($file_handle);
echo "File has been written successfully.<br>";
} else {
echo "Error: Unable to write to the file.<br>";
}
}
// reading from the file
if (file_exists($file_path)) {
$file_content = file_get_contents($file_path);
echo "<h3>File Content:</h3>";
echo "<pre>$file_content</pre>";
} else {
echo "Error: The file 'text.txt' does not exist in the 'doc' directory.";
}
?>
<!-- HTML form to take new content to write to the file -->
<form method="post" action="">
<label for="content">Enter content to write to the file:</label><br>
<textarea id="content" name="content" rows="4" cols="50"></textarea><br><br>
<input type="submit" value="Write to File">
</form>
<?php
echo '</pre>';
include('includes/footer.php');
?>