HomePHP Page 2 - Reading, Writing and Creating Files in PHP
File Permissions in a Unix Environment - PHP
Reading and writing to files can be useful if you do not require the storing of important data, such as a web counter. I must warn you though, that this method of storage should not be used to store passwords and other critical information, as it is not safe. Here we will discuss how to handle files and directories in PHP, specifically, how to create, read and write them.
In a Unix environment, all files and directories are owned by two different entities -- a user and a group. Each file in the File System has three permission sets that determine who can access a file or directory. These sets are: group-level, user-level and global-level. Each permission set has three flags: read, write and execute. If a user does not have, for example, a read flag set, he or she will not be able to read a particular file or directory. The same applies to a user that does not have a execute flag set; he or she will not be able to execute that script.
How to set the permissions
Permissions are set using a command called CHMOD. CHMOD stands for CHange MODe. CHMOD comes as a set of three numbers. Each of these three numbers is a sum total of three other numbers. So you have to add three numbers to get the first CHMOD number, add three numbers to get the second CHMOD number and add three numbers to get the third CHMOD number.
Let me explain. Each digit is a number value from 0 to 7. The value specifies what capabilities are available (or not). These numbers correspond to three command types: read(r), write(w) and execute(x).
Read (r) has a value of 4. It allows listing files in the directory. Write (w) has a value of 2. It allows the addition of new files to the directory. Execute (x) has a value of 1. It allows access to the files in the directory. Possible combinations available using these command types include:
write and execute
read and execute
read and write
read write execute
Sometimes you'll hear people say "chmod to 775."Okay... so what do the three digits stand for?
The first digit represents the host server/computer. This will usually be set to seven. The second digit represents the group. And the third represents the world or "others."
How do we arrive at the number 775? For the group "owner" we have: 1 [execute access] + 2 [write access] + 4 [read access] = 7.
For the group "groups" we have: 1 [execute access] + 2 [write access] + 4 [read access] = 7.
For group "others" we have: 1 [execute access] + 0 [no write access] + 4 [read access] = 5.
This means that the "host" and the "group" can do anything to the file, but anyone else can only execute it or read it. They can't modify it.
How and when do you use the CHMOD command?
The easiest way is by using an FTP program. Most FTP programs have a right-click menu that allows you to set the CHMOD on a specific folder. I use DreamWeaver 8; it provides a GUI on which I can set the permissions by right clicking on a file or directory, as below: