chmod()
If you’ve ever wondered why “chmod” was the name given to changing a file’s permissions, here you go. The “mod” is for “mode”; you’re changing the mode.
The mode can only be changed by the owner of the file. You’ll get a return value of false if this function fails to change the mode.
Be sure to give the mode as an octal. That means you’ll have four digits - the first one being a zero and the others 0-7.
What the numbers mean. If you can think in binary a little, it makes sense.
| Read | Write | Execute | VALUE |
| 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 1 |
| 0 | 1 | 0 | 2 |
| 0 | 1 | 1 | 3 |
| 1 | 0 | 0 | 4 |
| 1 | 0 | 1 | 5 |
| 1 | 1 | 0 | 6 |
| 1 | 1 | 1 | 7 |
So a value of 4 is read only; a value of 2 is write only. Add them together and you get 6 which means read and write.
But you need to give three numbers for the mode of a file. You’re setting the mode for three entities: owner, group, and everyone else - in that order.
$file = '/home/my_file.txt'; chmod( $file, 0644 ); /* owner: read + write group: read world: read */ chmod( $file, 0755 ); /* owner: all group: read + execute world: read + execute */
