Syntax: bool chmod ( string $filename , int $mode )
CHMOD comes as a set of 3 numbers. Each of these 3 numbers is a sum total of 3 other numbers. So you have to add 3 numbers to get the first CHMOD number, add 3 numbers to get the second CHMOD number and add 3 numbers to get the third CHMOD number.
Understanding the numbers...
Each digit is a number value from 0 to 7. The value specifies what capabilities are available (or not). These numbers correspond to 3 command types. Read, write and execute.
Read (r) has a value of 4. It allows listing files in the directory.
Write (w) has a value of 2. It allows the addition of new files to the directory.
Execute (x) has a value of 1. It allows access to the files in the directory.
Here are the possible combinations available using these command types:
Digit | rwx | Result |
0 | --- | no access |
1 | --x | execute |
2 | -w- | write |
3 | -wx | write and execute |
4 | r-- | read |
5 | r-x | read and execute |
6 | rw- | read and write |
7 | rwx | read write execute |
The first number represents the host server. This will usually be set to 7 giving the host full permission on the files in the folder. The second number represents the group (YOU - the individual being hosted). And the third number represents the world (the visitors to the site). Normally, on free hosts, these two digits will be set to 4, allowing the reading (and displaying) of files. Thus, no executing capabilities.
Typical settings for files are 777, 755, 666 or 644.
Typical settings for directories are 777 or 755.
CGI scripts 755, data files 666, and configuration files 644.
Make sure that you give the mode as an octal number. i.e.: 0777 (not just 777)
Example:
Code: Select all
<?php
$fp = fopen("myfile.txt", 'r');
if(!$fp) {
/* Must be 0444 (octal), NOT just 444 */
if(!chmod("myfile.txt", 0444)) {
echo "Error -- couldn't open myfile.txt for reading!";
exit;
} else {
$fp = fopen("myfile.txt", 'r');
}
}
$myvalue = fgets($fp, 1024);
fclose($fp);
?>
Code: Select all
<?php
function chmodr($path, $filemode) {
if (!is_dir($path))
return chmod($path, $filemode);
$dh = opendir($path);
while (($file = readdir($dh)) !== false) {
if($file != '.' && $file != '..') {
$fullpath = $path.'/'.$file;
if(is_link($fullpath))
return FALSE;
elseif(!is_dir($fullpath) && !chmod($fullpath, $filemode))
return FALSE;
elseif(!chmodr($fullpath, $filemode))
return FALSE;
}
}
closedir($dh);
if(chmod($path, $filemode))
return TRUE;
else
return FALSE;
}
?>