<!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" dir="ltr">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>.txt edit</title>
</head>
<body>
<?php
// the file to edit
$file 'test.txt';

if (
$_POST["edit"])  // file has been edited
{
    
// get new file contents
    
$edit $_POST["edit"];
    
    
// un-escape some characters
    
$edit str_replace(chr(92).chr(92), chr(92), $edit);
    
$edit str_replace("\'""'"$edit);
    
$edit str_replace(chr(92).chr(34), chr(34), $edit);

    
// Try to open file for writing
    
if(@$fopen fopen($file"w"))
    {
        
// write the file
        
fwrite($fopen$edit);
        
        
// close the file
        
fclose($fopen);
    }
    else
    {
        
// can't open the file
        
echo 'Error opening file';
    }
}


// Try to open file for reading
if(@$fopen fopen($file'r+'))
{
    
// read it's contents
    
if(filesize($file) > 0)
    {
        
$contents fread($fopenfilesize($file));
    }
    else
    {
        
$contents '';
    }
    
    
// output the contents into a textarea
    
echo
        
'<form method="post" action="' $_SERVER['PHP_SELF'] . '">' .
            
'<textarea rows="20" name="edit" cols="30">' .
                
$contents .
            
'</textarea>' .
            
'<br /><input type="submit" value="  Save  " />' .
        
'</form>';
    
    
// close the file
    
fclose($fopen);
}
else
{
    
// can't open the file
    
echo 'Error opening file';
}

?>
</body>
</html>