【问题标题】:Unzipped files change owner, php解压缩文件更改所有者,php
【发布时间】:2012-12-05 03:55:15
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: php unzip ziparchive file-ownership


    【解决方案1】:

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

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

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

    【讨论】:

    • 嗯,第一印象太深刻了。不过我会试试的。
    • 迈克,谢谢你的回答,但我不知道该怎么做,因为我没有足够的经验。但是放松,我正在尝试,然后我会尽快更新你:)
    • @manix 我很高兴你愿意尝试自己实现它。如果您遇到任何问题,请在此处回复,我很乐意提供一些指导。
    • @Mike_Brant,我发布了一个答案,可以做类似于我期望的事情,它没有完成,我不知道它是否是一个好的硬代码,但我认为它可能是一个好的开始。让它发挥作用是一场冒险,哈哈
    • 在您已经使用的同一台机器上使用 FTP 移动解压缩文件似乎很奇怪。它认为您遇到的问题实际上是目录和文件权限之一。您的 FTP 方法可能是一种解决方法,据我猜测,如果尝试在多个不同的主机上使用它将是一个非常脆弱的解决方案。我想我需要在这里了解您的用例,以了解您实际尝试执行的操作以及您是尝试从命令行还是通过 Web 服务器执行此操作。
    【解决方案2】:

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

    @Mike Brant 的回答

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

    创建流失败:权限被拒绝...

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

    FTP 功能

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

    1. 使用tmpfile() 创建一个临时文件。
    2. 使用ftp_fput() 将临时文件与压缩文件放在当前目录中。
    3. 使用ftp_siteCHMOD 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-01-04
      • 2015-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-16
      • 2013-11-13
      • 2015-10-03
      相关资源
      最近更新 更多