如何使用带目标目录的PHP创建压缩文件?

时间:2013-03-16 01:51:29

标签: php

我在以前的帖子中找到了以下脚本,用PHP创建了一个ZIP文件。

此脚本允许您创建zip文件,但不允许您在任何目录中创建它。这是原始代码。

    <?php
    // Config Vars 

    $sourcefolder =   "uploads/output" ; // 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 "<br>Archive ". $zipfilename . " created successfully - ";

    // And provide download link ?>
    <a href="http:<?php echo $zipfilename;?>" target="_blank">Download <?php echo $zipfilename?></a> 

我尝试修改代码以便能够决定脚本的设计,因为我在互联网上花了几个小时后才发现没有解决方案。一些解决方案建议在哪里更改$ zipfilename =“myarchive.zip”; to $ zipfilename =“uploads / output / myarchive.zip”;但在下载链接链接中我想显示为:存档myarchive.zip已成功创建 - 下载myarchive.zip 而不是存档上传/输出/ myarchive.zip已成功创建 - 下载myarchive。拉链

因此我尝试了这段代码,但它对我不起作用。我不知道我做错了什么。

<?php
// Config Vars 

$sourcefolder =   "uploads/output" ; // Default: "./" 
$destfolder  = "uploads/output/"; // Default: "myarchive.zip"
$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(''.$destfolder.''.$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 "<br>Archive ". $zipfilename . " created successfully - ";

// And provide download link ?>
<a href="http:<?php echo $zipfilename;?>" target="_blank">Download <?php echo $zipfilename?></a> 

1 个答案:

答案 0 :(得分:0)

创建zip存档的静态方法示例:

//...
public static function zip($source, $destination)
{
    if (!extension_loaded('zip') || !file_exists($source)) {
        throw new \Exception('zip not loaded');
    }

    $zip = new \ZipArchive();
    if ($zip->open($destination, \ZIPARCHIVE::CREATE) === false) {
        return false;
    }

    if (is_dir($source) === true) {
        $files = new \RecursiveIteratorIterator(
            new \RecursiveDirectoryIterator($source),
            \RecursiveIteratorIterator::SELF_FIRST
        );

        foreach ($files as $file) {
            if (in_array(substr($file, strrpos($file, DIRECTORY_SEPARATOR) + 1), array('.', '..'))) {
                continue;
            }
            $file = realpath($file);
            if (is_dir($file) === true) {
                $zip->addEmptyDir(str_replace($source . DIRECTORY_SEPARATOR, '', $file . DIRECTORY_SEPARATOR));
            } else {
                if (is_file($file) === true) {
                    $zip->addFromString(
                        str_replace($source . DIRECTORY_SEPARATOR, '', $file),
                        file_get_contents($file)
                    );
                }
            }
        }
    } else {
        if (is_file($source) === true) {
            $zip->addFromString(basename($source), file_get_contents($source));
        }
    }

    return $zip->close();
}
// ...