|
-
Oct 11th, 2002, 11:30 AM
#1
Thread Starter
Frenzied Member
chmod
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 Code:
<?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?
-
Oct 11th, 2002, 02:56 PM
#2
Stuck in the 80s
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):
Code:
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
-
Oct 11th, 2002, 04:45 PM
#3
Thread Starter
Frenzied Member
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.
-
Oct 11th, 2002, 05:55 PM
#4
Frenzied Member
fileperms will only return what you just chmoded it.
so it reads the most current permissions on the file
-
Oct 12th, 2002, 05:42 AM
#5
Thread Starter
Frenzied Member
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?
-
Oct 15th, 2002, 02:20 PM
#6
Frenzied Member
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 Code:
<?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>
<?
}
?>
-
Oct 16th, 2002, 05:17 AM
#7
Thread Starter
Frenzied Member
-
Oct 16th, 2002, 11:39 AM
#8
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 17th, 2002, 05:04 AM
#9
Thread Starter
Frenzied Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|