Results 1 to 9 of 9

Thread: chmod

  1. #1

    Thread Starter
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444

    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?

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    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
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3

    Thread Starter
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    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.

  4. #4
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    fileperms will only return what you just chmoded it.

    so it reads the most current permissions on the file

  5. #5

    Thread Starter
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    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?

  6. #6
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    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>
    <?
    }
    ?>

  7. #7

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  9. #9

    Thread Starter
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    Ah alright, thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width