Working with Permissions in PHP
Posted by Nessa | Posted in linux,php,tutorials | Posted on June 20, 2007
7
PHP uses the same command as *nix systems when dealing with changing permissions for files:
chown – changes ownership, but can only be done by a root user
chgrp – changes group ownership, can be done by a user who is a member of the new group
chmod – changes permissions, can be done by the user (and sometimes the group) that owns the file
These commands are particularly useful in situations where PHP runs as a different user on the system, which is common when PHP is compiled as an Apache user. A lot of our customers get frustrated at the fact that once they use PHP to create a file, their user can’t touch it. That’s why whenever you have PHP create a file that needs to be neutral, its permissions have to be set accordingly.
The syntax of those commands are simple:
chown($file, $user)
chgrp($file, $group)
chmod($file, $permissions)
The simplest example of using these commands is a follows:
<?php
$file = "myfile.txt";
$handler = fopen($file, 'w') or die("can't create file");
chmod($file, '0777');
fclose($handler); ?>
In this example, I had the PHP script create a file called ‘myfile.txt’ in write mode, then change its permissions to 777. This is of course the simplest example in the world, but you can make them much more complex.
For more information on using fopen to handle files, you can read this. Also, when you set permissions you have to use the octal value (0777) instead of just 777.
Related posts:










Actually, it’s not quite well to use chmod() while the file handler is still opened, but in many cases this will work.
Speaking in a round, it mostly depends on the system permissions for the “web-server user”. If your “server user” can’t write to a specific dir, then you can not too from PHP.
Where the commands are used mainly depends on their context in the script, but you are correct.
If a PHP script created the files, then it should be able to modify its own files. However, 1) if PHP runs as CGI then it maintains the same user identify when call from Apache and 2) when run PHP scripts via command line they run as whatever user is running them. These commands are mainly functional as a command line tool. It’s obvious to most users that if a script like this is called from command line, then its destination folder needs to be writable by the webserver.
These commands are just aliases for the equivalent system call.
A good rule of thumb for a target “writable” directory would be to chmod that initially to 755 (777 could be dangerous in some situations, but would also work).
Nessa’s Blog: Working with Permissions in PHP…
On her blog Nessa has a briefl look at working with ……
[...] her blog Nessa has a brief look at working with permissions with PHP via three functions – chown, chgrp and chmod. PHP uses the [...]
[...] in PHP Posted in December 24th, 2007 by admin in Uncategorized On her blog Nessa has a brief look at working with permissions with PHP via three functions – chown, chgrp and chmod. PHP uses the [...]
[...] 21st 2007 2:01am [-] From: v-nessa.net [...]