php - tar 提取后,更改权限

标签 php unix file-permissions tar

今天只是一个关于 unix 和 PHP 的问题。

我在我的 PHP 上所做的是使用 Unix 系统来解压缩压缩文件。

exec("tar -xzf foo.tar.gz");

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

Applications/
Library/
Systems/

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

这会导致 Permission denied (errno 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");

关于php - tar 提取后,更改权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2891902/

相关文章:

php - 保持表示 (HTML) 和逻辑 (PHP) 分离

php - Laravel Logging - 自定义 channel 不写

Linux 组权限

php - 如何使用 "MongoDB\Driver\Query($filter, $options)"?

linux - 使用 bash 对文件进行算术运算

linux - 我无法使用 sftp get 命令重命名我正在下载的文件

unix - 在 Unix 中为每条记录添加一个字符到文件的特定位置

linux - chmod 为 root 所有,权限为 220

linux - 如何在特定文件夹中设置默认组写权限

php - 如何在 PHP 中使用 CURL 获取 SSL 证书信息?