php – tar提取后,更改权限

关于unix和
PHP的问题今天.

我在PHP上做的是使用Unix系统解压缩tar文件.

exec(“tar -xzf foo.tar.gz”);

通常一切正常,直到遇到这个特定的foo.tar.gz,它有一个文件系统如下:

Applications/
Library/
Systems/

运行tar命令后,似乎文件权限变为644(而不是755).

这导致权限被拒绝(错误13),因此禁用了我的大部分代码. (我猜测缺乏特权)

我能以任何方式阻止这个tar命令完全破坏我的权限吗?

谢谢.

哦,这似乎只有在我有一个具有这个特定文件系统的foo.tar.gz文件时才会发生.别的什么,我很好.

最佳答案 如果要保留文件的权限,则必须在解压缩tarball时添加-p(或–preserve-permissions或–same-permissions)开关.来自tar手册页:

--preserve-permissions
--same-permissions
-p
    When `tar' is extracting an archive, it normally subtracts the
    users' umask from the permissions specified in the archive and
    uses that number as the permissions to create the destination
    file.  Specifying this option instructs `tar' that it should use
    the permissions directly from the archive.

所以PHP代码应该是:

exec("tar -xzfp foo.tar.gz");
点赞