【问题标题】:php chmod returns true for success but doesn't actually change permissionsphp chmod 成功返回 true 但实际上并没有更改权限
【发布时间】:2013-08-31 18:40:49
【问题描述】:

也许我不明白 php 的 chmod() 函数应该如何工作,但我遇到它返回 TRUE(表示成功)但实际上并未修改权限。

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

    $fn = $value["tmp_name"];
    $fps = fileperms($fn);

    $testMsg .= "file permissions are $fps\n";        
    $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: $didChmod\n";
    $fps = fileperms($fn);
    $testMsg .= "NEW file permissions are $fps\n";        
    $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() 报告成功但没有更改权限。

谢谢

【问题讨论】:

  • 它是否在支持权限的文件系统上?有时会遇到网络共享等问题
  • @Anigel - 是的,它是 linux。
  • 目录的权限/所有者是什么?另外,如果你 shell_exec chmod 而不是使用内置的 php 会发生什么?
  • @susiederkins 我认为可能发生的是服务器本身不允许您更改权限,因为它属于“服务器”而不是所有者本身。 tmp 文件一旦成功moved 就会被自动删除。尝试使用上传的文件本身测试您的 chmod,您会发现它可以工作。 这是我的理论。
  • @Anigel /tmp 目录由 root 拥有,权限为 777。有趣的是,shell_exec 使 chmod 工作。为什么会这样?

标签: php permissions chmod


【解决方案1】:

来自手册

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

如果此脚本由网络服务器运行(即通过浏览器访问),则此 PHP 脚本将作为错误用户运行。只有文件的所有者(或root)才能使用chmod,网络服务器可能以www-data或其他方式运行,因此没有chmod的权限。

【讨论】:

  • 为什么trim(shell_exec('whoami')) 会作为文件的所有者返回?看来脚本的所有者/执行者文件的所有者。
  • 我对结果的解释与@Kirk 的相同——php 脚本作为www-data 运行,它也是文件所有者。所以应该有chmod文件的权限
猜你喜欢
  • 2020-05-03
  • 2023-03-20
  • 2020-12-05
  • 2019-05-15
  • 1970-01-01
  • 1970-01-01
  • 2013-02-16
  • 1970-01-01
  • 2011-08-04
相关资源
最近更新 更多