2

我有一个 zip 文件到这样的目录中:

drwxr-xr-x 2 salome  salome  4096 Dec 16 17:41 staff.zip

当我使用ZipArchive类解压缩文件时,所有压缩文件都是nobody用户所有者。有没有办法避免这种所有者变更?

  1. 如果需要,我可以使用 ftp(仅作为 salome 用户)。
  2. 该脚本最终将在多个主机上共享,因此我们的想法是使其尽可能通用。
4

2 回答 2

2

您可能会考虑扩展zipArchive该类并覆盖该extractTo方法以对目录中的文件也执行 a chown()

根据您在评论中讨论的用例,您可能还需要考虑使用 Phar 存档格式。php.net/manual/en/intro.phar.php

Phar 将允许您的模块提交者提交您根本不需要提取的可执行 PHP 代码的文件文件。

于 2012-12-18T00:37:56.230 回答
0

好的,我已经解决了nobody用户的问题。我将尝试解释我所有的解决方法。

@迈克布兰特的回答

Mike 建议我使用chown()函数覆盖extractTo()方法。好吧,在愿意使用它之前,我chown()不断地测试了独立的功能,它打印了错误:

未能创建流:权限被拒绝...

看起来 chown 不适用于主要的共享主机

FTP功能

因此,尽管FTP functions我制作了一个运行良好的脚本,但至少现在 xD 继续前进。这是脚本对一个压缩文件所做的简历:

  1. 使用tmpfile().
  2. 用于将ftp_fput()临时文件与压缩文件一起放在当前目录中。
  3. ftp_site使用和授予写入权限CHMOD 0777
  4. 使用 .读取压缩文件内容$content = $zip->getFromName('zipped-file.txt');
  5. 使用 . 将内容放入新文件fputs($fp, $content);
  6. 关闭连接

下面的代码说明了完整的过程

$zip = new ZipArchive;
$ftp_path_to_unzip = '/public_html/ejemplos/php/ftp/upload/';
$local_path_to_unzip = '/home/user/public_html/ejemplos/php/ftp/upload/';

if ($zip->open('test.zip') == TRUE) {
    
    //connect to the ftp server
    $conn_id = ftp_connect('ftp.example.com');
    $login_result = ftp_login($conn_id, 'user', 'password');    
    
    //if the connection is ok, then...
    if ($login_result) {
        
        //iterate each zipped file
        for ($i = 0; $i < $zip->numFiles; $i++) {
            $filename = $zip->getNameIndex($i);
            
            //create a "local" temp file in order to put it "remotely" in the same machine
            $temp = tmpfile();
            
            //create the new file with the same name from the extracted file in question
            ftp_fput($conn_id, $ftp_path_to_unzip . $filename, $temp, FTP_ASCII);
            
            //set write permissions, eventually we will put its content
            ftp_site($conn_id, "CHMOD 0777 " . $ftp_path_to_unzip . $filename);
            
            //open the new file that we have created
            $fp = fopen($local_path_to_unzip . $filename, 'w');
            
            //put the content from zipped file
            $content = $zip->getFromName($filename);
            fputs($fp, $content);
            
            //close the file
            fclose($fp);
            
            //now only the owner can write the file
            ftp_site($conn_id, "CHMOD 0644 " . $ftp_path_to_unzip . $filename);
            
        }
    }
    
    // close the connection and the file handler
    ftp_close($conn_id);
    
    //close the zip file
    $zip->close();
}

这是开始更复杂的自定义的第一步,因为上面的代码无法知道压缩文件是“目录”还是“文件”。

于 2012-12-18T16:53:03.810 回答