Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12

Thread: chmod()?

  1. #1
    Regular Coder
    Join Date
    Jul 2004
    Posts
    134
    Thanks
    0
    Thanked 0 Times in 0 Posts

    chmod()?

    How can I change permission of a directory?
    chmod() can change permission of a file only?
    I wanna use a PHP script and change permission of a directory and then write a new (non-existing) file in it. Please help.

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I had some problems myself setting file and folder permissions with chmod so I used the following solution and it works for me - if you can chmod via ftp it should hopefully work for you, too:

    PHP Code:
    $target '/path/from/root/to/file/or/directory/';
    $chmod_setting '0777'// the permission to set (don't miss the leading 0)

    if(!$ftp_connect = @ftp_connect(ftp_server)) {
        echo 
    'Could not connect to ftp server';
    }

    if (!@
    ftp_login($ftp_connectftp_userftp_pass)) {
        echo = 
    'Could not login with username and password';
    }

    $chmod_cmd "CHMOD " $chmod_setting " " $target;

    if(!
    $chmod = @ftp_site($ftp_connect$chmod_cmd)) {
        echo 
    'The file/folder permissions could not be changed';

    } else {
        echo 
    'New file permissions set';


  • #3
    Regular Coder
    Join Date
    Nov 2004
    Location
    The Netherlands
    Posts
    551
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So the php chmod command can change file modes, while the ftp_site can echo ftp commands like fput and fget, so chmod on the server can change directory modes?

    It's kind of funny you have to let the server talk to himself with ftp.
    CATdude about IE6: "All your box-model are belong to us"

  • #4
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm not entirely sure but I think that chmod() can be limited in the php.ini. On my host I can't use chmod at all, either to change file or directory permissions, but I can use php to login as an ftp user and do exactly the same thing without restriction, weird huh

  • #5
    Regular Coder
    Join Date
    Jul 2004
    Posts
    134
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Very surprising, PHP has no function to change directory functions
    We use delinear's code so.

  • #6
    Regular Coder
    Join Date
    Jul 2004
    Posts
    134
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh! I'm getting error: The file/folder permissions could not be changed
    Why this error and only this?

    I echoed $target, it is: /home/jewelry/public_html/test.html

    So the file isn't created

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Are you perhaps running in safe_mode? Check your php.ini settings. Safe_mode is affecting the way how chmod works. From the manual:

    Note: When safe mode is enabled, PHP checks whether the files or directories you are about to operate on have the same UID (owner) as the script that is being executed. In addition, you cannot set the SUID, SGID and sticky bits
    And of course you can change permissions on directories too via chmod(). At least on a *NIX console you can, I don't see any reason why PHP wouldn't be able to do so. The error stems from a mismatch between the FTP user's permissions and the file owner's permissions. Double-check that the FTP user has the write-permission enabled for the file in question.
    De gustibus non est disputandum.

  • #8
    Regular Coder
    Join Date
    Jul 2004
    Posts
    134
    Thanks
    0
    Thanked 0 Times in 0 Posts
    phpinfo() shows:
    Code:
    safe_mode Off Off 
    safe_mode_exec_dir no value no value 
    safe_mode_gid Off Off 
    safe_mode_include_dir

  • #9
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Does the file actually already exist or are you trying to create it? I got a bit confused by your previous post.

  • #10
    Regular Coder
    Join Date
    Jul 2004
    Posts
    134
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Actually, I'm writing a CMS, so I need create/edit/delete files. Is it unsafe, if I give write permission to complete public_html directory? Also, can you suggest a friendly, light-weight yet robust cms ( unlike Mambo, Nuke; these are heavy ).

  • #11
    Regular Coder
    Join Date
    Jul 2004
    Posts
    134
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK, I've given write permission to public_html and it's all fine now. Another problem poped: I'm using FCKEditor for CMS and it's converting all my PHP code to HTML!! Please help me.

  • #12
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Regarding one of your last questions: Yes, it's unsafe to give write permissions to everyone (owner, group, others) in your whole public_html directory. Try to limit permissions as much as possible. Depends a little on your actual server/hosting setup though, sometimes one needs to chmod a file to 0777.

    Those last questions of you have nothing to do with chmod, so I suggest you open seperate threads for them.
    De gustibus non est disputandum.


  •  

    Posting Permissions

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