File Manipulations

To read, write or append to a file, the file must be opened. At the end of the file access, the file must be closed.

$file = fopen (filepath, access_code);

fclose ($file);
The filepath is a string that gives the path and the name of the file. The access_code is a string that gives the mode in which the file is opened. The possible access codes are:
"r" Read only. The file pointer is positioned at the beginning of the file.
"w" Write only. The file pointer is positioned at the beginning of the file. Previous content of an existing file is erased. Creates a new file if it does not exist.
"a" Write only. The file pointer is positioned to the end of the file. Creates a new file if the file does not exist.

PHP provides several tests on a file. These built-in functions takes the filename as a parameter.

file_exists Returns True is file exists.
is_readable Returns True if the file is readable.
is_writable Returns True if the file is writable.
is_executable Returns True if the file is executable.
is_file Returns True if the file is a file and not a directory.
filesize Returns the size in bytes
filemtime Returns the number of days since last modification.
fileatime Returns the number of days since last access.
filectime Returns the number of days since last creation.

There are several ways to read a file. One can read one line at a time or read the whole file at once. To read a file one line at a time do:

$line = fgets ($file, max_bytes);
The max_bytes parameter is optional. If omitted its default value is 1024 bytes. The function fgets() reads a single line up to the next new line character or until the max_bytes is reached. There is also an end of file (feof()) function that tests for the end of file. One way of reading a file line by line until the end is as follows:
while (!feof($file))
{
  $line = fgets($file);
  // do something
}
The whole file can be read at once using fread(). This function needs the size of the file which can be obtained with the filesize() function:
$whole_file = fread ($file, filesize($file));

In PHP you can write to a file that has been opened. The two functions are equivalent - fwrite() or fputs(). The function takes two parameters - the name of the file and the output string. It returns the number of bytes written.

$bytes_written = fwrite($file, $output_string);

Directories can be read in PHP. First a handle to the directory is created and then the names of the files in the directory can be read using the function readdir().

$dir = opendir ($directory_name);
while ($file = readdir ($dir))
{
  // do something
}
closedir ($dir);

Environment Variables

The function phpinfo() returns all the environment variables and their values. The ones that we shall be concerned with are:

$_SERVER['HTTP_USER_AGENT'] Returns the browser on the client side. Useful for browser sniffing.
$_SERVER['REMOTE_ADDR'] Has the IP address of the client.
$_POST Hash of name-value pairs from the POST method.
$_GET Hash of name-value pairs from the GET method.
$_SESSION Session variables
$_COOKIE Hash of name-value pairs from cookies.

Form Handling

The action attribute in a form will have the url of the PHP script that will process the form on the server side. Make sure that the permission on that script has been set 755. Depending on the method - GET or POST - the name value pairs of the form data can be extracted from the hash $_GET or $_POST.

$value = $_POST['name'];
For form data where a single name is associated with multiple values like a pull down menu with multiple selections there is a work around.
<form method = "POST" action = "<?php echo $_SERVER['PHP_SELF']; ?>">
Select some desserts:
<select name = "dessert[]" multiple = "multiple">
<option value = "iceCream"> Vanilla Ice Cream </option>
<option value = "pie"> Apple Pie </option>
<option value = "cake"> Chocolate Cake </option>
<option value = "custard"> Creme Brulee </option>
<option value = "jello"> Jello </option>
</select>
<input type = "submit" name = "Order">
</form>
If you select Vanilla Ice Cream and Apple Pie and submit the form then $_POST['dessert'] is itself and array. Thus
$dessertList = $_POST['dessert'];
$dessertList[0] will have iceCream and $dessertList[1] will have pie.

Notice the use of $_SERVER['PHP_SELF'] as the value of the action attribute. PHP_SELF holds the name of the current script. This indicates that the script that generated the form is also the script that processes it.

Pattern Matching in PHP

There are two kinds of pattern matching in PHP. One is based on POSIX regular expressions and the other is based on Perl regular expressions. To use Perl regular expressions the library PCRE (Perl Compatible Regular Expression) must be installed. There are several PHP functions that do pattern matching using Perl style regular expressions.

The preg_match() function takes two and optionally three parameters. The first parameter is the regular expression, the second parameter is the string that is being searched, and the third is the array of matches that were found. The return type of this function is boolean.

preg_match ("/pattern/", $string, $results_array)

The preg_match_all() performs a global regular expression match. After the first match is found, the subsequent searches are continued on from the end of the last match.

preg_match_all ("/pattern/", $string, $match_array, $flags)
The flags could be PREG_PATTERN_ORDER or PREG_SET_ORDER.

The preg_replace() performs a regular expression search and replace.

preg_replace ("/pattern/", $replacementString, $resultingString, $numReplacements)
The number $numReplacements is a limit to the number of replacements that are made.

The preg_split() splits a string by a regular expression.

preg_split ("/pattern/", $string)

The preg_grep() function returns an array of elements that match a pattern in a given array.

preg_grep ("/pattern/", $inputArray)