php/e08FReadWrite.php

<form action="<?php echo basename(__file__);?>" method="post">
<h2><?php echo basename(__file__);?> php output</h2>
<?php 
$oo = "";
$tt = 'enter file';
$isRd = isset($_POST['rw']) ? $_POST['rw'] == 'read' : true; 
$fn = isset($_POST['file']) ? $_POST['file'] : '';
if (! $fn ) {
} elseif ( $isRd) {
    $f = fopen("readWrite/$fn", 'r');
    $oo .= "\nfopen $f";
    if ($f) {
        $tt = '';
        $lC = 0;
        $cC = 0;
        $oo .= "\nreading $fn, replacing newlines by \\n";
        while ($li = fgets($f)) {
            $lC++;
            $cC += strlen($li);
            $tt .= ($lC == 1 ? '' : "\n") . str_replace(["\r\n", "\n\r", "\n", "\r"], ["\\n", "\\n","\\n","\\n"], $li);
        } 
        $rc = fclose($f);
        $oo .= "\n$lC lines, $cC chars\nfclose($f) $rc";
    }         
} else if ( isset($_POST['cont']) ) {
    $f = fopen("readWrite/$fn", 'w');
    $oo .= "\nfopen $f";
    if ($f) {
        $co = $_POST['cont'];
        $oo .= "\nwriting to $fn, " . substr_count($co, "\n") . " \\n, " . substr_count($co, "\r") . " \\r, "
            . substr_count($co, "\r\n") . " \\r\\n, " . substr_count($co, "\n\r") . " \\n\\r, " . strlen($co) . " chars";
        $rc = fwrite($f, $co);
        $oo .= "\nwritten $rc chars";
        $rc = fclose($f);
        $oo .= "\nfclose($f) $rc";
    }
} else {
    $tt = 'cont not set';
}

# $a = $b->c; #test php error message output
if ($oo != "")
    echo "<li>" . str_replace("\n", "</li><li>", substr($oo, 1)) . "</li>";
?>
<h2>form</h2>
 <p>file: <input type="text" name="file" value="<?php echo $fn;?>"> relative <?php echo realpath('readWrite'); ?></p>
 <p><input type="radio" name="rw" value="read" <?php if ( $isRd ) echo 'checked'?> > read 
    <input type="radio" name="rw" value="write" <?php if ( ! $isRd ) echo 'checked'?> > write
 <p><input type="submit" /></p>
 <textarea name="cont" rows="20" cols="140"><?php echo $tt; ?></textarea>
<h2>Source</h2>
<?php 
echo __file__ . "<br><br>\n";
highlight_file( __file__); 
?>
</form>