Hudzilla.org - the homepage of Paul Hudson
Contents > Files > Permissions Wish List | Report Bug | About Me ]

8.12.1     Setting permissions: chmod()

This is NOT the latest copy of this book; click here for the latest version.

bool chmod ( string filename, int mode)

Note: this section applies only to those using PHP on a Unix-like operating system. The reason for this is because Windows has a vastly different security system to 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.

If you are using a Unix box, you can use PHP's chmod() function like you would use chmod from the command line, with the difference that you need to precede the security level with a 0 to show that it is an octal value. Chmod() takes two parameters: the file to set, and the value to set it to. Here are two examples:

<?php
    chmod
("/var/www/myfile.txt", 0777);
    
chmod("/var/www/myfile.txt", 0755);
?>

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





<< 8.12 Permissions   8.12.2 Changing file ownership: chown() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
A PHP User - 05 Dec 2008

If you're writing a script that's going to be distributed, some hosts may not allow php scripts to chmod other files. You should then preceed the chmod function with an '@' symbol to supress any error messages.

A PHP User - 05 Dec 2008

755 means readable and executable by everyone. full access to owner



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is seven plus three?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow