Veronica's Reader-Appender:

This script finds/creates and reads/writes a file

Looking for file...
File found!

Checking for readability...
File is readable -- reading the file.
File reads: Text, Text, Text , Text, Text , Text, Text , Text, Text, and more Text ! ! Can't seem to get enough text! Can't seem to get enough text! text

Checking for append-ability...
File is appendable -- appending the file.
Writing~ ~to the file.




 

CODE FOLLOWS

<?php
include('includes/header.php');

// set error reporting to all
error_reporting(E_ALL);

// variable being passed via POST ternary operator
$str_write = isset($_POST['message']) ? $_POST['message'] : '';

// scrub input
$str_write htmlspecialchars($str_write);

echo(
"<h1>Veronica's Reader-Appender:</h1>");

echo(
'<h2>This script finds/creates and reads/writes a file</h2>');

$myFile '../docs/read_append.txt';

// does file exist?
echo('Looking for file... <br>');
if(
file_exists($myFile))
{
    echo(
'File found! <br>');
}
else
{
    echo(
'File is NOT there -- creating a new file. <br>');

    
// create a new file
    
$handle fopen($myFile'w') or die('Could not create file.');
    
fwrite($handle'');
    
fclose($handle);
}
echo(
'<br>Checking for readability... <br>');

// try reading the contents of the file
$str_read file_get_contents($myFilefalsenull0filesize($myFile));

if(
$str_read)
{
    echo(
'File is readable -- reading the file. <br>');
    echo(
"File reads: $str_read<br>");
}
else
{
    echo(
"Cannot read the $myFile ! <br>");
}
echo(
'<br>Checking for append-ability... <br>');

// try appending the file
$handle fopen($myFile'a');
if(
$handle)
{
    echo(
'File is appendable -- appending the file. <br>');

    
// write the new text to the file
    
fwrite($handle$str_write);
    
fclose($handle);
    echo(
"Writing~ $str_write ~to the file.<br>");
}
else
{
    echo(
"Cannot write to the $myFile !");
}

?>

<br><br>
<form action="" method="POST">
    <label for="message">Message</label>
    <textarea id="message" name="message" rows="4" cols="50"><?php echo($str_write); ?>
        </textarea>
    <br><br>
    <input type="submit" name="submit" value="Submit">
</form>
<?php

include('includes/footer.php');
?>