more fun with file io

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

1
.gitignore vendored

@ -1,3 +1,4 @@
*.log *.log
*.pid *.pid
docroot/nginx.conf docroot/nginx.conf
docroot/html/writable/*.*

@ -40,5 +40,74 @@ mkheader('files!', '#aaa');
?></pre> ?></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> </body>
</html> </html>

Loading…
Cancel
Save