Click to See Complete Forum and Search --> : chmod
Rick Bull
Oct 11th, 2002, 11:30 AM
I want to Chmod a file so it is writeable, and then restore it's old permissions. Will fileperms return the original permissions, and then can I chmod the file with what it returns like so:
<?php
$perms = fileperms('./FILENAME.php');
chmod('./FILENAME', 0777);
chmod('./FILENAME', $perms);
?>
I was told I may need to convert $perms to octal, is that righ?
The Hobo
Oct 11th, 2002, 02:56 PM
Attempts to change the mode of the file specified by filename to that given in mode.
Note that mode is not automatically assumed to be an octal value, so strings (such as "g+w") will not work properly. To ensure the expected operation, you need to prefix mode with a zero (0):
chmod ("/somedir/somefile", 755); // decimal; probably incorrect
chmod ("/somedir/somefile", "u+rwx,go+rx"); // string; incorrect
chmod ("/somedir/somefile", 0755); // octal; correct value of mode
Rick Bull
Oct 11th, 2002, 04:45 PM
I have read that already, but do you know if fileperms returns the the same as what you use in CHMOD, i.e. the read, write and execute permissions? The manual isn't very clear on that.
phpman
Oct 11th, 2002, 05:55 PM
fileperms will only return what you just chmoded it.
so it reads the most current permissions on the file
Rick Bull
Oct 12th, 2002, 05:42 AM
But if I do it before I chmod it obviously it will return what it was then right? So should I use decoct on $perms to get it to octal?
phpman
Oct 15th, 2002, 02:20 PM
why worry about octal?
just chmod it to 777
do what you want to it and then chmod it back to orignal, how hard is that. atually by looking at it you might. try this, cookie.php is just an empty file. thsi is just a littel script I wrote to test if chmod was working. you might find it useful.
<?php
function grabPermissions($file,$sub) {
$decperms = fileperms($file);
$octalperms = sprintf("%o",$decperms);
return $perms=(substr($octalperms,$sub));
}
/*
$file = is the filename/directory
$sub = 2 for directory, 3 for file
*/
$file = "cookie.php";
if ($_REQUEST['submit']){
exec("chmod ".$_REQUEST['chmd']." cookie.php");
$text = grabPermissions($file,3);
echo "Before Changing: ".$_REQUEST['text3'];
echo "<br>After Chmoding: ".$text;
echo"<form action=\"chmod.php\" method=\"post\">";
echo"number to change it to: <input type=\"text\" name=\"chmd\" size=\"10\">";
echo"<input type=\"submit\" name=\"submit\" value=\"change chmod\">";
echo"<input type=\"hidden\" name=\"text3\" value=\"$text\">";
echo"</form>";
} else {
$text2 = grabPermissions($file,3);
echo "File is Currently Chmoded at: ".$text2;
?>
<form action="chmod.php" method="post">
number to change it to: <input type="text" name="chmd" size="10">
<input type="submit" name="submit" value="change chmod">
<input type="hidden" name="text3" value="<?php echo $text2 ?>">
</form>
<?
}
?>
Rick Bull
Oct 16th, 2002, 05:17 AM
OK thanks.
CornedBee
Oct 16th, 2002, 11:39 AM
Rick: use your method (it's faster) and don't worry about octal. Numbers are stored binary, not decimal/hexadecimal/octal. The filesystem only cares about the binary representation. It will use 9 bits (= 3 octal digits) to determine the permissions. Octal is just a very handy method to write it, that's why it's used. But you could for example write
chmod("file", 0777);
or
chmod("file", 511);
or
chmod("file", 0x1FF);
which is all the same in binary (111111111).
It's just that octal is so handy...
In case of variables you don't need to worry about the number system - there is none.
Rick Bull
Oct 17th, 2002, 05:04 AM
Ah alright, thanks.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.