dongqin1075 2011-11-11 08:05
浏览 196
已采纳

在php中更改目录权限(chmod)

I have googled but I can't seem to get my script to work.

this is my code

if (is_dir("tmp")) {
    if (substr(sprintf("%o", fileperms("tmp")), -4) == "0777") {
        echo "good";
    } else {
        echo "going to chmod the tmp folder to 777";
        if (!chmod("tmp", octdec(0777))) { // tried chmod("tmp, 0777) too
                echo "Oops, I couldn't chmod the /setup/tmp directory, please do this manually";
        }
    }
} else {
    echo "we'll make the folder";
}

I can't seem to get the chmod to work, i read somewhere that if register globals was off then this wouldn't work(i have that setting set to off).

I followed the PHP.net manual and some of the examples they provided there in the user comments, as well as some stack-overflow posts that i found relevant. but perhaps i need to tweak some php settings to get this to work?

Is there a way to get the chmod function to work without needing to change the PHP.ini?

  • 写回答

3条回答 默认 最新

  • dpzjl68484 2011-11-11 08:14
    关注

    You haven't given us enough information to determine why chmod() is failing. It could be that the web-server doesn't have sufficient privileges to alter directory permissions.

    The owner of the web-server process should own the directory or be a member of the owning group. For example, if root owns tmp, and your web-server is running as the user 'apache', it will not be possible to modify the directory's permissions from PHP.

    I believe the settings you mention in php.ini are for debugging, not magically 'fixing' chmod. Try prepending the following lines of code to your PHP script and you should get more useful debug output:

    ini_set('display_errors', 'On');
    error_reporting(E_ALL)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?