This lab will let you practice some file operations using PHP.
Part One: Displaying PHP Source
One difficult aspect of PHP is that unlike HTML and JavaScript, it is not easy to see the PHP source when you visit a page. Since the server has already interpreted the script, the original file can no longer be seen. For this part of the lab you will write a function that will take any file and display it in the browser as is, without any script or markup interpretation. You will test the script by calling your function on a few of the files you have created for previous labs -- both PHP and HTML sources.
Note that you might think it a good idea to create a more general PHP script, that could take an argument as from a form: viewfile.php?file=index.php. While this is easy to do, and would no doubt be useful, it is nonetheless not a good idea. Why? What files would be visible via this script?
Here's an outline of how the function can work:
- You can read the file into an array using the file function.
- Loop through the file and display the results. Make sure that you sanitize the output so that it is not interpreted as any HTML tags!
- Wrap the whole thing in an HTML shell inside a <pre> tag.
There may be other ways to accomplish the same task. What else can you think of?
Part Two: Reading a Data File
For this exercise, we'll read a text file that contains a simple table of data, and format it for output. Normally we would want to store our data in a database rather than a text file, but sometimes this is impossible. For example the text file represents the output of some other program. In this case we will use the file /etc/printcap, which is produced automatically on Unix systems and lists information on the available printers. Here is what the file looked like when this lab was produced. (Note: if you want to try this lab on a non-unix computer, you can copy the content below into a text file and direct your script there.)
# This file was automatically generated by cupsd(8) from the # /etc/cups/printers.conf file. All changes to this file # will be lost. ford243|ford243:rm=beowulf.csc.smith.edu:rp=ford243: burton_302|burton_302:rm=beowulf.csc.smith.edu:rp=burton_302: burton_303|burton_303:rm=beowulf.csc.smith.edu:rp=burton_303: burton_forum|burton_forum:rm=beowulf.csc.smith.edu:rp=burton_forum: cats|cats:rm=beowulf.csc.smith.edu:rp=cats: ford243_color|ford243_color:rm=beowulf.csc.smith.edu:rp=ford243_color: ford321|ford321:rm=beowulf.csc.smith.edu:rp=ford321: ford354|ford354:rm=beowulf.csc.smith.edu:rp=ford354:
Let's say we want to produce a list of the names of all the printers on the system. These are stored in the beginning of each line, up to the vertical pipe character |. (The first three lines are comments and should be ignored.) So you could do something like the following:
- Read the file into an array of lines.
- Loop through the array of lines. If the line is a comment, skip it. Otherwise, extract the part of the line that is the printer name and echo it.