cleaning up file io bits a touch

cat-town
Dan Buch 14 years ago
parent 3617a08430
commit 295db7cee7

@ -2,16 +2,56 @@
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('data.txt', 'r');
$handle = fopen($DATASTORE, 'r');
fclose($handle);
print "didn't actually do anything here ...";
@ -22,7 +62,7 @@ mkheader('files!', '#aaa');
<pre><?php
// get everything!
echo file_get_contents('data.txt');
echo file_get_contents($DATASTORE);
?></pre>
@ -30,7 +70,7 @@ mkheader('files!', '#aaa');
<!-- ======================================================= -->
<pre><?php
$lines = file('data.txt');
$lines = file($DATASTORE);
foreach($lines as $key => $line) {
$linenum = $key + 1;
@ -44,7 +84,7 @@ mkheader('files!', '#aaa');
<!-- ======================================================= -->
<pre><?php
$handle = fopen('data.txt', 'r');
$handle = fopen($DATASTORE, 'r');
$first24chars = fread($handle, 24);
fclose($handle);
@ -53,61 +93,5 @@ mkheader('files!', '#aaa');
?></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>

Loading…
Cancel
Save