box-o-sand/docroot/html/files.php

114 lines
2.2 KiB
PHP
Raw Normal View History

2010-12-30 18:57:54 +00:00
<?php
require_once('_head.php');
mkheader('files!', '#aaa');
?>
<body>
<!-- ======================================================= -->
<pre><?php
// do nothing!
$handle = fopen('data.txt', 'r');
fclose($handle);
2010-12-31 06:46:02 +00:00
print "didn't actually do anything here ...";
2010-12-30 18:57:54 +00:00
?></pre>
<!-- ======================================================= -->
<pre><?php
// get everything!
echo file_get_contents('data.txt');
?></pre>
2010-12-31 06:46:02 +00:00
<!-- ======================================================= -->
<pre><?php
$lines = file('data.txt');
foreach($lines as $key => $line) {
$linenum = $key + 1;
print "Line $linenum: $line";
}
?></pre>
2010-12-31 07:29:03 +00:00
<!-- ======================================================= -->
<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
}
?>
2010-12-30 18:57:54 +00:00
</body>
</html>