box-o-sand/PracticingPhp/web/html/wikipedia-tutorial/files.php

98 lines
1.8 KiB
PHP

<?php
require_once('_head.php');
$DATASTORE = 'writable/datastore.txt';
mkheader('files!', '#aaa');
?>
<body>
<!-- ======================================================= -->
<?php
if (file_exists($DATASTORE)) {
$data = file_get_contents($DATASTORE);
} else {
$data = "";
}
if ($_SERVER['REQUEST_METHOD'] == "GET") {
?>
<form name="datastore" action="" method="POST">
<textarea name="data" rows="10" cols="80"><?php
print $data;
?></textarea>
<input type="submit" name="submit" value="Save" />
</form>
<?php
} else {
$data = $_POST['data'];
$handle = fopen($DATASTORE, 'w');
fwrite($handle, str_replace(array("\r\n"), "\n", $data));
fclose($handle);
?>
<p>Saved!</p>
<p>
<em><a href="<?php echo $_SERVER['PHP_SELF']; ?>">clear this message</a></em>
</p>
<?php
}
?>
<!-- ======================================================= -->
<pre><?php
// do nothing!
$handle = fopen($DATASTORE, 'r');
fclose($handle);
print "didn't actually do anything here ...";
?></pre>
<!-- ======================================================= -->
<pre><?php
// get everything!
echo file_get_contents($DATASTORE);
?></pre>
<!-- ======================================================= -->
<pre><?php
$lines = file($DATASTORE);
foreach($lines as $key => $line) {
$linenum = $key + 1;
print "Line $linenum: $line";
}
?></pre>
<!-- ======================================================= -->
<pre><?php
$handle = fopen($DATASTORE, 'r');
$first24chars = fread($handle, 24);
fclose($handle);
print $first24chars;
?></pre>
</body>
</html>