PHP chmod 返回 true 表示成功,但实际上并没有更改权限



也许我不理解php的chmod()函数应该如何工作,但我发现它返回TRUE(表示成功),但实际上并没有修改权限。

我正在处理一个已上传到web服务器tmp目录的文件。

    $fn = $value["tmp_name"];
    $fps = fileperms($fn);
    $testMsg .= "file permissions are $fpsn";        
    $testMsg .= "(which is " .  substr(sprintf('%o', $fps), -4) . ")n";
    $arr = posix_getpwuid(fileowner($fn));
    $testMsg .= "file owner is " . $arr["name"] . "n";
    $testMsg .= "running as: " . trim(shell_exec('whoami')) . "n";

    //can i chmod it?
    $didChmod = chmod($fn, 0644);
    $testMsg .= "chmod: $didChmodn";
    $fps = fileperms($fn);
    $testMsg .= "NEW file permissions are $fpsn";        
    $testMsg .= "(which is " .  substr(sprintf('%o', $fps), -4) . ")n";

以上输出为:

file permissions are 33152
(which is 0600)
file owner is www-data
running as: www-data
chmod: 1
NEW file permissions are 33152
(which is 0600)

正如您所看到的,chmod()报告成功,但没有更改权限。

感谢

从手动

当前用户是运行PHP的用户。可能不是与您用于正常shell或FTP访问的用户相同。模式可以是只有在大多数系统上拥有该文件的用户才能更改。

如果此脚本由Web服务器运行(即通过浏览器访问),则此PHP脚本将以错误用户身份运行。只有文件的所有者(或root)可以使用chmod,Web服务器可能以www数据或其他形式运行,因此没有chmod的权限。

相关内容

最新更新