|
home—lects—exams—hws
breeze (snow day)
From PHP Visual Quickstart Guide by Larry Ullman
Based on notes by Jack Davis (jcdavis@radford.edu)
there is a tower of increasingly sophisticated ways to share state between pages:
There are two primary ways of storing data with PHP; using files (and directories) or databases. This chapter will discuss the former, and the next chapter will introduce the latter. Although a database can be more powerful and secure than a file-based system, it is surprising how much can be done by sending and retrieving information from simple text documents on the server.
Writes the new data to the file in accordance with the selected mode
(appending to previous
Normally, each piece of data to be written on its own line, so each submission should conclude with the appropriate line break for the operating system of the computer running PHP. For *nix (incl. RU, Mac OSX) that's \n; for DOS/Windows (and a few others), that's \r\n (and for Mac OS 9-, it was just \r, sigh!).
flock($fp,LOCK_EX); fwrite($fp, 'data to be written...\n'); flock($fp,LOCK_UN); |
$fp = fopen('myData.txt', 'r'); while (!feof($fp)) { $string = fgets($fp, 1024); // Read until end-of-line (or 1023 chars: imagine room for a \n) (or EOF) } fclose($fp); |
$allLines = file('.\ch11-files-sayings.txt'); // $allLines[0] now contains the first line of the file. |
<input name="UploadedFile" type="file" /> <input type="submit" value="UPLOAD" />These cause the file to be uploaded, using both an invented temporary name (to avoid clashes), plus remembering the file's original name. Within our PHP, use the superglobal
Note that this script uses
1
As opposed to
home—lects—exams—hws
breeze (snow day)
©2011, Ian Barland, Radford University Last modified 2011.Mar.23 (Wed) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |