box-o-sand/php-practice/web/html/wikipedia-tutorial/files.php

98 lines
1.8 KiB
PHP
Raw Normal View History

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