Section 13.11. Changing File Permissions and Ownership


13.11. Changing File Permissions and Ownership

PHP's chmod( ) function is vaguely similar to the Unix chmod command, but you must always specify the permissions using octal values; you can specify just one filename; and you specify that filename before the permission setting. As you are using octal values, you need to precede the security level with a 0. This function takes two parameters: the file to set and the value to set it to.

The chmod( ) function is available only to those using PHP on a Unix-like operating system. This is because Windows has a vastly different security system than Unix, where privileges are handed out by user and user group. Whereas Unix users can say "Read only for user, read-write for group," Windows users on Windows 95, 98, and ME can only say "Read only" or "Not read only." PHP does not support the fine-grained Windows NT/2000/XP/2003 access model.

Here are two examples:

     chmod("/var/www/myfile.txt", 0777);     chmod("/var/www/myfile.txt", 0755); 

Line one sets the file to readable, writable, and executable by all users, whereas line two sets the file to readable, writable, and executable by owner, and just readable and writable by everyone else.

The chown( ) function is quite rarely used in PHP, as you must have administrator privileges to change the ownership of a file. However, on the command line chown( ) is sometimes helpful, and it attempts to change the file passed in parameter one so that it is owned by the user specified in parameter two. On success, TRue is returned; otherwise, false. The second parameter can either be a username or a user ID number. For example:

     if (chown("myfile.txt", "sally")) {             print "File owner changed.\n";     } else {             print "File ownership change failed!\n";     } 

Note that both chmod( ) and chown( ) only work on local filesystems.



PHP in a Nutshell
Ubuntu Unleashed
ISBN: 596100671
EAN: 2147483647
Year: 2003
Pages: 249

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