【发布时间】: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