cleaning up file io bits a touch

This commit is contained in:
Dan Buch 2010-12-31 02:32:41 -05:00
parent 3617a08430
commit 295db7cee7

View File

@ -2,79 +2,18 @@
require_once('_head.php');
$DATASTORE = 'writable/datastore.txt';
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);
if (file_exists($DATASTORE)) {
$data = file_get_contents($DATASTORE);
} else {
$data = "";
}
@ -93,7 +32,7 @@ mkheader('files!', '#aaa');
} else {
$data = $_POST['data'];
$handle = fopen($datastore, 'w');
$handle = fopen($DATASTORE, 'w');
fwrite($handle, str_replace(array("\r\n"), "\n", $data));
fclose($handle);
@ -105,9 +44,54 @@ mkheader('files!', '#aaa');
<?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>