Setting permissions

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.

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:

    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.


Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Changing file ownership >>

Previous chapter: Permissions

Jump to:


Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.