In this post, you will learn what is file permission, how it works, how to modify file permission, check file permission, symbolic and octal notation of file permission and more…
I’ll also provide you a FREE File Permission Changer tool, written in PHP – by me.
What do you mean by Read, Write and Execute a file/folder?
Read
The read permission grants the ability to read a file. When set for a directory, this permission grants the ability to read the names of files in the directory (but not to find out any further information about them such as contents, file type, size, ownership, permissions, etc.)
Write
The write permission grants the ability to modify a file. When set for a directory, this permission grants the ability to modify entries in the directory. This includes creating files, deleting files, and renaming files.
Execute
The execute permission grants the ability to execute a file. This permission must be set for executable binaries (for example, a compiled C++ program) or shell scripts (for example, a Perl program) in order to allow the operating system to run them. When set for a directory, this permission grants the ability to access file contents and metainfo if its name is known, but not list files inside the directory (unless read is set).
File Permission Table – Alphabetical values
Permission | User | Group | Other |
Read (r) | Yes | Yes | Yes |
Write (w) | Yes | Yes | No |
Execute (x) | Yes | No | No |
Result | rwx | rw- | r– |
Permission | User | Group | Other |
Read | r | r | r |
Write | w | – | – |
Execute | x | – | – |
Result | rwx | rw- | r– |
- r means read permission yes
- w means write permission yes
- x means execute permission yes
- – means read/write/execute permission denied
File Permission Table – Numerical values
Permission | User | Group | Other |
Read | 4 | 4 | 4 |
Write | 2 | 2 | 0 |
Execute | 1 | 0 | 0 |
Result | 7 | 6 | 4 |
- Read permission yes = 4
- Write permission yes = 2
- Execute permission yes = 1
- Read/write/execute permission denied = 0
File Permission Notations
Symbolic notation
So, symbolic notation to express file permission is: rwxrw-r–
Octal Notation
and octal notation is: 764 as above table.
Check file permission with PHP
You can check file permission from the PHP function below. This function will give you file permission in octal notation.
<?php function check_file_perm ($filename) { return substr(decoct(fileperms($filename)), 2); } echo check_file_perm('file-name.txt'); // output will be in this format: 0666 ?>
To get file permission in alphabetical notation, you can use the following function.
<?php $perms = fileperms('filename.txt'); if (($perms & 0xC000) == 0xC000) { // Socket $info = 's'; } elseif (($perms & 0xA000) == 0xA000) { // Symbolic Link $info = 'l'; } elseif (($perms & 0x8000) == 0x8000) { // Regular $info = '-'; } elseif (($perms & 0x6000) == 0x6000) { // Block special $info = 'b'; } elseif (($perms & 0x4000) == 0x4000) { // Directory $info = 'd'; } elseif (($perms & 0x2000) == 0x2000) { // Character special $info = 'c'; } elseif (($perms & 0x1000) == 0x1000) { // FIFO pipe $info = 'p'; } else { // Unknown $info = 'u'; } // Owner $info .= (($perms & 0x0100) ? 'r' : '-'); $info .= (($perms & 0x0080) ? 'w' : '-'); $info .= (($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x' ) : (($perms & 0x0800) ? 'S' : '-')); // Group $info .= (($perms & 0x0020) ? 'r' : '-'); $info .= (($perms & 0x0010) ? 'w' : '-'); $info .= (($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x' ) : (($perms & 0x0400) ? 'S' : '-')); // World $info .= (($perms & 0x0004) ? 'r' : '-'); $info .= (($perms & 0x0002) ? 'w' : '-'); $info .= (($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x' ) : (($perms & 0x0200) ? 'T' : '-')); echo $info; ?>
Modify file permission with PHP
To modify file permission of a file, just use the following function.
<?php chmod("filename.txt", 0755); ?>
Add my own file permission checker & changer tool
This PHP script is not coded well, I made this just for testing purpose.
<html> <head> <title>file permission checker & changer tool by Arpan Das (http://w3epic.com)</title> </head> <body style="font-family: 'trebuchet ms', arial, tahoma"> <center> <h1>file permission checker & changer tool by Arpan Das (<a href='http://w3epic.com'>http://w3epic.com</a>)</h1> <div style="margin:0% 30%; text-align:left;font-family:'trebuchet ms', arial;"> <?php ioi_set('display_errors', '0'); // file permission checker if (isset($_POST['submit1'])) { $check_file = $_POST['check_file']; if (file_exists($check_file)==true) { echo "permission of <span style='color:red;'>{$check_file}</span> is <span style='color:green;'>".substr(decoct(fileperms($check_file)), 3)."</span>"; } else { echo "<span style='color:red;'>File not found</span>"; } } // file permission changer if (isset($_POST['submit2'])) { $file = $_POST['filename']; $perm = $_POST['perm']; $perm = '0'.$perm; if (file_exists($file)==true) { $success = "permission of <span style='color:red;'>{$file}</span> has been successfully set to <span style='color:red;'>{$perm}</span>"; $failed = $file . " failed to set permission " . $perm; if (chmod($file, $perm)) echo $success; else echo $failed; } else { echo "<span style='color:red;'>File not found</span>"; } } ?> <hr /> <h3>File permission checker:</h3> <form action='PHP_file_permission_changer.php' method='post'> Filename:<input type='text' name='check_file' value='<?=$check_file?>' /> <input type='submit' name='submit1' value='submit' /> </form> <hr /> <h3>File permission changer:</h3> <form action='PHP_file_permission_changer.php' method='post'> Filename: <input type='text' name='filename' value='<?=$file?>' /> Permission: <input type='num' name='perm' value='<?=$perm?>' /> (3 digit octal format) <input type='submit' name='submit2' value='submit' /> </form> <hr /> </div> © <?=date('Y')?> Arpan Das (<a href='http://w3epic.com'>http://w3epic.com</a>) </center> </body> </html>
You can download the above code from here.
If you have any question, please leave a comment, I’ll be back to you.
Thank you!