When chmod'ing via a script, what you have to remember is that your web server usually runs with barely ANY permissions. For example, Most apache server's will run as the user "nobody". As such, any script that is running on this server, will also be running as the user "nobody", with the same permissions. So, for apache to change the permissions on a script not owned by the user "nobody", it must have world writeable permissions, ie, chmod 777
Unix permissions work like this:
A typical permission string for a file looks like this: -rwxr-xr-x
There are 4 sections there. The first specifies the file type. This section is 1 character long. It can be -, which is a normal file, d, which is a directory, l, which is a link, and many more.
The second section is 3 characters long (rwx). This tells what permissions the OWNER of the file has. Typically, this is read, write, and execute.
The 3rd section, is once again 3 characters long, and is the permissions that the GROUP has.
The last section, 3 chars long, is the permissions that EVERYBODY has.
So, if you have a file, owned by root, and it's group is webadmins, and you want root and web admins full access, but everybody to just have read and execute you would want the string:
-rwxrwxr-x
however, your web server wont be able to write to this. Only the owner and the group will. If you change it to -rwxrwxrwx everybody has full access to that file, and the web server will be able to write to it.
This php function will display the permissions of a file in unix format:
function get_perms($file) {
/* This function gets the permissions of a file and displays them in standard
UNIX format (drwxrwxr-x) */
$p_bin = substr(decbin(fileperms($file)), -9) ;
$p_arr = explode(".", substr(chunk_split($p_bin, 1, "."), 0, 17)) ;
$perms = ""; $i = 0;
foreach ($p_arr as $this) {
$p_char = ( $i%3==0 ? "r" : ( $i%3==1 ? "w" : "x" ) );
$perms .= ( $this=="1" ? $p_char : "-" ) . ( $i%3==2 ? " " : "" );
$i++;
}
return $perms;
}
Hope that helps,
Matt