ICS 325
There are two ways to save data in PHP; using a
database and using flat files.
A flat file is simply a text file.
The basic steps in file processing
are:
Read
data from file.
Close
the file.
Writing Open the file. //
Create file if missing, and handle any error
Write
data to the file.
Close
the file.
The most common function for opening
files in PHP is fopen().
fopen (string
fileName, string fileMode [, int use_include_path])
fileName = a string value that
specifies where the file is located.
fileMode = How the file will be
opened.
use_include_path = look for the file in the include path if not found.
|
Mode |
Description |
|
r |
Access
the file for reading only, start access from beginning of the file. |
|
r+ |
Access
the file for both reading and writing, start access from beginning of the
file. |
|
w |
Access
the file for writing only, If the file exists, erase all contents. If file
does not exist, an attempt will be made to create the file. In either case
access the file from the beginning. |
|
w+ |
Access
the file for both reading and writing, If the file exists erase all contents.
If the file does not exist, an attempt will be made to create the file. In
either case, access the file from the beginning. |
|
a |
Open the
file for writing only. If the file does not exist, an attempt will be made to
create it. If the file does exist, access will start from the end of the file
(no erasing). |
|
a+ |
Open the
file for reading and writing. If the file does not exist, an attempt will be
made to create it. If the file does exist, access will start from the end of
the file (no erasing). |
One of the basic file input functions
in PHP is fgets()
fgets (int
filePointer [,int length])
filePointer
= a reference to the opened file
length [optional] = the length of data
that will be read from the file line. If
this line is omitted by default 1024 bytes are used. The file will be read until the passed in
length, or default value, is met; an end of file is met; or a new line is met.
feof
(int filePointer) returns a true value when the end of the file is reached.
filePointer = a reference to the opened file
One of the basic file output functions
in PHP is fputs()
fputs (int
filePointer, string outputString [,int length])
filePointer
= a reference to the opened file.
outputString
= the string that will be written to file.
length [optional] = the length of data
that will be written the file line.
If the file
does exist, chmod a+w for the file.
The most common function for closing
files in PHP is fclose().
fclose
(int filePointer)
filePointer = the reference to the
file that was opened.
Example:
<?php
$filePointer = fopen (“/tmp/myFile.txt”,”w+”);
echo “Line 1: ”fgets ($filePointer);
$newText = “Add this line to the file”;
fputs ($filePointer, $newText);
fclose ($filePointer);
?>
Searching
is difficult
Concurrent
access is not reliable
Random
access is not available
Cannot
set different levels of access to different parts of the data
RDBMS
$myArray = array(12345, 23456, 34567, 45678);
$myArray = array ( “number1”=>12345, ”number2”=>23456,
”number3”=>34567);
$myArray = array (array(123,456,789)
array(134,468,824)
array(321,654,987));
Multidimensional Associative Array Example:
<?php
$cpu
= array(
"AMD"=>array("price"=>"125.00","speed"=>"1.6
GHz","type"=>"Athlon XP"),
"INT"=>array("price"=>"135.00","speed"=>"1.7
GHz","type"=>"Pentium 4")
);
echo
"An AMD ".$cpu['AMD']['type']." ";
echo $cpu['AMD']['speed']." costs $";
echo $cpu['AMD']['price'];
echo
"<br />";
echo
"An Intel".$cpu['INT']['type']." ";
echo
$cpu['INt']['speed']." costs $";
echo $cpu['INT']['price'];
?>
An
AMD Athlon XP 1.6 GHz costs $125.00
An Intel Pentium 4 1.7 GHz costs $135.00
assort () associative array sorting on values
ksort () associative array sorting on keys
rsort () normal array sorting in reverse
order
krsort () associative array sorting on keys in
reverse order
arsort () associative array sorting on values
in reverse order
usort () user defined sort, function accepts
the array and a sorting function
usort
() user
defined sort for associative arrays, function accepts the array and a sorting
function
Example: function compare ($x, $y) {
if
($x == $y)
return 0;
else
if ($x < $y)
return -1;
else
return 1;
}
$nums ( 10, 5, 15, 25);
usort ( $nums, compare );