box-o-sand/docroot/html/files.php
2010-12-31 02:29:03 -05:00

114 lines
2.2 KiB
PHP

<?php
require_once('_head.php');
mkheader('files!', '#aaa');
?>
<body>
<!-- ======================================================= -->
<pre><?php
// do nothing!
$handle = fopen('data.txt', 'r');
fclose($handle);
print "didn't actually do anything here ...";
?></pre>
<!-- ======================================================= -->
<pre><?php
// get everything!
echo file_get_contents('data.txt');
?></pre>
<!-- ======================================================= -->
<pre><?php
$lines = file('data.txt');
foreach($lines as $key => $line) {
$linenum = $key + 1;
print "Line $linenum: $line";
}
?></pre>
<!-- ======================================================= -->
<pre><?php
$handle = fopen('data.txt', 'r');
$first24chars = fread($handle, 24);
fclose($handle);
print $first24chars;
?></pre>
<!-- ======================================================= -->
<pre><?php
$outfile = 'writable/fileio-' . time() . '-' . rand() . '.txt';
$handle = fopen($outfile, 'w');
$outdata = $_GET['out'] ? $_GET['out'] . "\n" : "Like this?\n";
fwrite($handle, $outdata);
fclose($handle);
print "<a href=/$outfile>wrote stuff</a>";
?></pre>
<!-- ======================================================= -->
<?php
$datastore = 'writable/datastore.txt';
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
}
?>
</body>
</html>