【问题标题】:compress/archive folder using php script使用 php 脚本压缩/归档文件夹
【发布时间】:2010-09-30 06:46:45
【问题描述】:

有没有办法使用 php 脚本将服务器中的文件夹压缩/归档为 .zip 或 .rar 或任何其他压缩格式,以便我们可以根据要求归档文件夹,然后提供下载链接

提前致谢

【问题讨论】:

    标签: php compression


    【解决方案1】:

    这是一个例子:

    <?php
    
    // Adding files to a .zip file, no zip file exists it creates a new ZIP file
    
    // increase script timeout value
    ini_set('max_execution_time', 5000);
    
    // create object
    $zip = new ZipArchive();
    
    // open archive 
    if ($zip->open('my-archive.zip', ZIPARCHIVE::CREATE) !== TRUE) {
        die ("Could not open archive");
    }
    
    // initialize an iterator
    // pass it the directory to be processed
    $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("themes/"));
    
    // iterate over the directory
    // add each file found to the archive
    foreach ($iterator as $key=>$value) {
        $zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key");
    }
    
    // close and save archive
    $zip->close();
    echo "Archive created successfully.";
    ?>
    

    【讨论】:

    • 很棒的代码...我怎样才能编辑这个压缩嵌套文件夹,不包括通向它的所有文件夹?我想要的文件夹位于“/var/www/vhosts/mysite.com/dev/wp-content/themes/mytheme/”。当我运行这个脚本时,我得到一个 var 的起始文件夹,然后是 www,然后是 vhosts,然后是 mysite.com 等等。我错过了什么?
    • 我是否正确理解代码也通过“。”进行迭代和“主题/”内的“..”目录?当您尝试解压缩存档时,这似乎会导致问题。
    【解决方案2】:

    注意 Adnan 示例中可能存在的问题:如果目标 myarchive.zip 位于源文件夹中,则需要在循环中排除它,或者在创建存档文件之前运行迭代器(如果没有) t 已经存在)。这是一个使用后一个选项的修改后的脚本,并在顶部添加了一些配置变量。这个不应该用于添加到现有档案中。

    <?php
    // Config Vars 
    
    $sourcefolder = "./"           ; // Default: "./" 
    $zipfilename  = "myarchive.zip"; // Default: "myarchive.zip"
    $timeout      = 5000           ; // Default: 5000
    
    // instantate an iterator (before creating the zip archive, just
    // in case the zip file is created inside the source folder)
    // and traverse the directory to get the file list.
    $dirlist = new RecursiveDirectoryIterator($sourcefolder);
    $filelist = new RecursiveIteratorIterator($dirlist);
    
    // set script timeout value 
    ini_set('max_execution_time', $timeout);
    
    // instantate object
    $zip = new ZipArchive();
    
    // create and open the archive 
    if ($zip->open("$zipfilename", ZipArchive::CREATE) !== TRUE) {
        die ("Could not open archive");
    }
    
    // add each file in the file list to the archive
    foreach ($filelist as $key=>$value) {
        $zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key");
    }
    
    // close the archive
    $zip->close();
    echo "Archive ". $zipfilename . " created successfully.";
    
    // And provide download link ?>
    <a href="http:<?php echo $zipfilename;?>" target="_blank">
    Download <?php echo $zipfilename?></a> 
    

    【讨论】:

      【解决方案3】:

      PHP 带有 ZipArchive 扩展名,非常适合您。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-30
        • 2012-01-02
        • 1970-01-01
        • 2010-09-20
        • 2020-08-31
        • 1970-01-01
        相关资源
        最近更新 更多