Online Book Reader

Home Category

HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [233]

By Root 1373 0
function.

} // end foreach

//print the bottom of the table

print “ \n”;

These shortcuts — the file() function and list() — make it very easy to work with CSV data. That’s one reason this type of data is popular for basic data problems.

The list() construct works only on numerically indexed arrays and assumes that the array index begins at 0. If you want to use the list() function with associative arrays, surround the array variable with the array_values() function. Technically, list() is not a function but a language construct. (See http://us3.php.net/list for more information on the list() function.)

The file() function is appealing, but it isn’t perfect for every situation. It’s great as long as the file size is relatively small, but if you try to load in a very large file, you will run into memory limitations. The “line at a time” approach used in readContact.php doesn’t have this problem because there’s only a small amount of data in memory at any given time.

Escaping with HTML entities

If you’re planning on displaying the user’s input to the screen, escape all the special characters before saving the user’s input to a file or sending it to the browser. Otherwise, some malicious user could use some simple CSS and HTML to really mess up your page. Remember: Paranoia is your friend. The simplest way to guard against this is to use the htmlentities() function:

$userInput = htmlentities($userInput);

This function converts any HTML characters the user may have entered into the character’s HTML entities equivalent. That is, if the user entered

, it’d be converted to <div>. When you display it back to the page, instead of creating a new HTML div, the browser will simply output the literal string
to the user.

If, for some reason, you want to decode these entities, use the html_entity_decode() function. This works exactly like its htmlentities() counterpart, just in reverse.


Working with File and Directory Functions

Sometimes, you may need PHP to work with files in a directory. Say you have a reporting tool for a client. Each week, you generate a new report for the client and place it in a directory. You don’t want to have to alter the page each time you do this, so instead, make a page that automatically generates a list of all the report files for the client to select from. This is the kind of thing you can do with functions like openddir() and readdir().


opendir()

Using the opendir() function, you can create a variable (technically speaking, this type of variable is called a handle) that allows you to work with a particular directory.

The opendir() function takes one parameter: the path to the directory you want to work with. The opendir() function returns a directory handle (kind of like a connection to the directory) on success and False on failure.

Here is an example of the opendir() function (see the “Generating the list of file links” section to see the opendir() function in action). This function stores a directory handle to the C:\xampp\htdocs\XFD\xfd5.7 directory in the $directoryHandle variable:

$directoryHandle = opendir(“C:\xampp\htdocs\XFD\xfd5.7”);


readdir()

After you open the directory with the opendir() function, you have a cursor pointed at the first file. At this point, you can read the filenames one by one with a while loop. To do this, use the readdir() function.

The readdir() function takes one parameter; the variable containing the directory handle created with the opendir() function. The readdir() function returns the name of a file currently being focused on by the cursor on success and False on failure.

Here is an example of the readdir() function. This function iterates through each file in the directory specified by $dp and assigns the filename of the current file to a new index in $fileArray array:

while($currentFile !== false){

$currentFile = readDir($dp);

$filesArray[] = $currentFile;

}

The actual readdir() function itself is readdir($dp). For more on the readdir() function, see the official PHP online documentation at

®Online Book Reader