The sources I learned from were not very clear about how PHP handles file permissions. They all talked about "owner" and "group" without explaining what they mean by owner and group. I expected "owner" to be me but what owner really means is the server. "Group" is who creates the file. If you create a file, "group" is you. If a PHP script creates a file, "group" is PHP. Since you and PHP are two different groups, you must change the world permissions to 7 if you want both yourself and PHP to be able to edit a file.
Permissions are set using a command called chmod(). chmod() stands for CHange MODe. Chmod is a set of three numbers. Each of those numbers is the sum of three other numbers. Those numbers can be 1, 2 or 4 and mean different things depending on if they are applied to a file or directory
| Files | Directory | |
|---|---|---|
| 1 | Executable | Allows access to the files in the directory. |
| 2 | Writable | Allows addition of new files |
| 4 | Readable | Allows listing of files |
Combining 1, 2 and 4 gives you the following possible settings:
| rwx | Result | |
|---|---|---|
| 0 | --- | no access |
| 1 | --x | execute |
| 2 | -w- | write |
| 3 | -wx | write and execute |
| 4 | r-- | read |
| 5 | r-x | read and execute |
| 6 | rw- | read and write |
| 7 | rwx | read write execute |
A typical setting for a file is 644; The server can read and write to the file and you and the world can read it. When you use your file manager to create the file, the server is the owner so the file manager is the owner. If you try to edit the file with PHP, it won't work because you are not the owner.
One other important point to make note of is that permissions are in octel, not decimal. Later on, if you try to check permissions with fileperms() will not be what you expect. For example, if the chmod is 777, you'll get 1411. You'll need to use decoct() on the output to convert it an octel number to get the results you expect..
|
|
|