Editing text files with PHP

Editing text files with PHP

Editing a file using a textarea

Sometimes you can't download/upload a file but must instead edit a file online. You can use a textarea to display the contents of a file, edit the file, then save your changes to the file that you opened.

You should should have this script in a secured directory and add code to make sure it is not used in a manner that you would not want it to be used. Be cautioned that this script could cause serious harm, depending on the code that is added to the edited file.

<?php
 
if ($submit) {
$fp = fopen("data.txt", "w");
fwrite($fp, stripslashes($newdata));
fclose($fp);
}
 
$fp = fopen("data.txt", "r");
while (!feof($fp)) {
$data .= fgets($fp, 4096);
}
fclose($fp);
 
?>
 
<html>
<head>
<title>Untitled</title>
</head>
 
<body>
<form action="<? print $PHP_SELF; ?>" method="post">
<textarea name="newdata" rows="10" cols="40">
<?
print $data;
?>
</textarea>
 
<input type="submit" name="submit" value="Submit">
</form>
 
</body>
</html>