【问题标题】:How to unzip a folder and delete zipped original?如何解压缩文件夹并删除压缩的原始文件?
【发布时间】:2011-02-17 17:15:21
【问题描述】:

我正在为我的 CMS 创建简单的安装程序\设置程序。当用户使用我的 CMS 下载 .zip 并将其解压缩到他的 php 服务器上的某个文件夹中时,他会看到一个文件 - install.php 和一个名为 - Contents.zip 的 zip 文件夹,我需要一些 php 函数来从该 Contents.zip zip 文件中提取文件而不是删除该文件。 (如果可能的话,我想在文件夹解压缩后立即为从那里提取的文件\文件夹赋予不同的权限)

这样的事情怎么办?

【问题讨论】:

    标签: php zip unzip delete-file rights-management


    【解决方案1】:

    您可以使用PHP ZipArchive library 来达到您的目的。

    另外,code example from the documentation 您可能会觉得有用。

    <?php
    
    $zip = new ZipArchive();
    $filename = "./test112.zip";
    
    if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
        exit("cannot open <$filename>\n");
    }
    
    $zip->addFromString("testfilephp.txt" . time(), "#1 This is a test string added as testfilephp.txt.\n");
    $zip->addFromString("testfilephp2.txt" . time(), "#2 This is a test string added as testfilephp2.txt.\n");
    $zip->addFile($thisdir . "/too.php","/testfromfile.php");
    echo "numfiles: " . $zip->numFiles . "\n";
    echo "status:" . $zip->status . "\n";
    $zip->close();
    ?>
    

    要更改文件权限,您可以使用PHP builtin function chmod.

    例如

    chmod("/somedir/somefile", 0600);
    

    删除你可以使用的文件,php builtin unlink

    例如,

    unlink('test.html');
    unlink('testdir');
    

    我强烈建议你阅读官方 PHP 文档。

    【讨论】:

    • 是官方 php 5.2 默认 apache php 的“PHP ZipArchive 库”的一部分吗?
    • 你在哪里删除那个 zip 文件?
    • @Ole 是的,ZipArchive 带有官方发行版;但是它对zlib.net有一个外部依赖
    • 所以当有人从某个供应商处购买 Apache Web Hosting 时,他可能无法使用此功能?
    猜你喜欢
    • 1970-01-01
    • 2010-09-05
    • 2013-03-09
    • 1970-01-01
    • 2010-12-14
    • 2019-02-02
    • 2019-05-07
    • 1970-01-01
    • 2015-10-03
    相关资源
    最近更新 更多