Recipe 24.3. Changing File Permissions or Ownership


24.3.1. Problem

You want to change a file's permissions or ownership; for example, you want to prevent other users from being able to look at a file of sensitive data.

24.3.2. Solution

Use chmod( ) to change the permissions of a file, as shown in Example 24-10.

Changing file permissions

<?php chmod('/home/user/secrets.txt',0400); ?>

Use chown( ) to change a file's owner and chgrp( ) to change a file's group. These are shown in Example 24-11.

Changing file owner and group

<?php chown('/tmp/myfile.txt','sklar');           // specify user by name chgrp('/home/sklar/schedule.txt','soccer'); // specify group by name chown('/tmp/myfile.txt',5001);              // specify user by uid chgrp('/home/sklar/schedule.txt',102);      // specify group by gid ?>

24.3.3. Discussion

The permissions passed to chmod( ) must be specified as an octal number.

The superuser can change the permissions, owner, and group of any file. Other users are restricted. They can change only the permissions and group of files that they own, and can't change the owner at all. A non-superuser can also change only the group of a file to a group to which the user belongs.

The functions chmod( ), chgrp( ), and chown( ) don't work on Windows.

24.3.4. See Also

Documentation on chmod( ) at http://www.php.net/chmod, chown( ) at http://www.php.net/chown, and chgrp( ) at http://www.php.net/chgrp.




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

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