PHP for the World Wide Web (Visual QuickStart Guide)
I l @ ve RuBoard |
Before I go through the examples of writing to, and reading from, a file, I should briefly mention file permissions. This is another topic (like security and regular expressions) which is large enough that you may want to pursue it furtherin which case see Appendix C, PHP Resources but I'll cover enough to get you started. File and directory permissions identify who can do what to the file or directory. The options are: read , write , and execute (actually, files can be designated executable whereas directories are made searchable ). On top of that, these options can be set differently for three unique types of users: the owner of the file, i.e., the person who put it on the server; members of a particular group , which a server administrator sets; and, others , i.e., those who don't fall into the previous two categories. There is also the implied everyone level which includes all of the above users. Normally a file's owner is given read and write permissions by default, while others are able to read a file ( groups don't really come into play here). Execution of a file is a major security issue and fortunately doesn't affect PHP scripts (Perl can write an executable file but PHP generally cannot, since it is a scripting language, not a programming language). Being able to write to a file can also be a security issue and should only be designated an option when absolutely necessary. In this chapter, you will be working with a text file on the server named data.txt. Depending upon your server configuration (e.g., if you are working on an ISP's server or your own home computer), you may want to set the permissions on this file ahead of time. If the file does not have the correct permissions to do what your PHP script is asking it to, you will see an error message similar to that in Figure 10.1. Figure 10.1. The "permission denied " message is the result of attempting to do something to a file which is not allowed by the server. Here the server is denying the fopen() function which is attempting to open data.txt for the purposes of writing to it.
Before proceeding through this chapter, you should create data.txt on the server.
Tip If you are already familiar with telnet and chmod , you probably also understand what the 0666 number means (Figure 10.2), but I'll explain for those of you who aren't familiar with it. The is a necessary prefix and then each 6 corresponds to write ( 4 ) plus read ( 2 ) permissionfirst assigning 6 to the owner , then to the group , then to others . Comparatively, 0777 allows write ( 4 ) plus read ( 2 ) plus execute ( 1 ) permission to all three types of user . This is applicable for Unix variant operating systems only (e.g. Linux and Solaris). Figure 10.2. At the shell prompt in the directory where data.txt is located, the line chmod 0666 data.txt changes the permissions for the file. You can check the permissions by typing ls l. This command reveals that: data.txt has read and write permis-sions for all three categories of users; the file's owner is ullman; and the file's size is 375 bytes.
To create data.txt :
This may seem like an odd series of steps, but in order to set the permissions on a file, it must exist first. You do want the file to be blank, though, as you'll use PHP to write data to it later. Depending upon your situation, there are a number of ways you can set a file's permissions. The desired end result for this example is to give either others or everyone permission to read and write (but not execute ) data.txt. You will need to check with your ISP as to how to set file permissions, but most likely it will involve one of the following options: To set a file's permissions:
|
I l @ ve RuBoard |