File Permissions

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.

graphics/10fig01.jpg

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.

graphics/10fig02.jpg


To create data.txt :

  1. Open your text editor and create a new document.

  2. Without typing anything into the file, save it as data.txt.

  3. Upload the file to your server.

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:

  • Some ISPs offer their users a Web-based control panel where they can set file permissions, as well set other hosting parameters.

  • If you have telnet access to the server, navigate to the directory where data.txt is located and use the chmod command (on a Unix server) to alter its permissions ( Figure 10.2 ). See the tip below for more information about setting permissions using chmod on Unix servers.

  • You may be able to change a file's permissions using your FTP client ( Figure 10.3 ).

    Figure 10.3. The Fetch FTP application for the Macintosh uses this pop-up window to allow you to set a file's permissions.

    graphics/10fig03.gif

  • If you are working on your own Windows NT server, you can alter a file's permissions by right-clicking on the file and selecting "Properties," then clicking on the "Security" tab.

I l @ ve RuBoard


PHP for the World Wide Web (Visual QuickStart Guide)
PHP for the World Wide Web (Visual QuickStart Guide)
ISBN: 0201727870
EAN: 2147483647
Year: 2001
Pages: 116
Authors: Larry Ullman

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net