ZipArchive - zip未创建,但不会抛出任何错误

时间:2015-03-31 10:31:58

标签: php ziparchive

我正在努力用ZipArchive创建拉链。我修改了一个函数,让我从一组文件路径创建一个zip。

您是否可以发现此代码中的任何错误,阻止创建zip文件的任何内容?

// Function to create a zip archive for the low res images
function create_zip_lres($files = array(), $destination = '', $overwrite = false) {
    // If the zip file already exists and overwrite is false, return false
    if(file_exists($destination) && !$overwrite) { return false; }

    $valid_files = array();

    // If files were passed in
    if(is_array($files)) {
        foreach($files as $file) {
            // Make sure the file exists
            if(file_exists($file)) {
                $valid_files[] = $file;
            }
        }
    }

    // If we have good files
    if(count($valid_files)) {

        // Create the archive
        $zip = new ZipArchive();

        if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
            return false;
        }

        // Add the files
        foreach($valid_files as $file) {
            $basename = basename($file);
            $zip->addFile($file, $basename);
        }

        // DEBUG
        echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;

        if ($zip->close()) {
            echo ' - Success!';
            echo '<br>';
            echo 'Your zip path is: ' . $destination;
        } else {
            echo ' - Failed!';
        }

        // DEBUG
        echo '<br>';
        print_r($zip);

        // Return filepath for zip
        return $destination;
    }
    else {
        return false;
    }
}



// getting the images array
$resize_images = resize_images();
// destination path
$lres_directory = get_lres_full_upload_dir() . get_the_title() . '-low-res.zip';

if (function_exists('create_zip_lres')) {
    create_zip_lres($resize_images, $lres_directory);
} else {

}

我添加了一些调试行来找出发生了什么,虽然它告诉我一切都很好,但没有创建文件。

The zip archive contains 3 files with a status of 0 - Success!
Your zip path is: http://media.mysite.com/wp-content/uploads/lres-download-manager-files/Knus 1400-low-res.zip
ZipArchive Object ( [status] => 0 [statusSys] => 0 [numFiles] => 0 [filename] => [comment] => )

2 个答案:

答案 0 :(得分:2)

我有另一个动态数组值的解决方案输入到zip文件。我已经测试过这个工作正常。

希望这会有所帮助:

<?php
//Our Dynamic array
$array = array( "name" => "raaaaaa",
    "test" => "/sites/chessboard.jpg"
);

//After zip files are stored in this folder
$file= "/sites/master.zip";

$zip = new ZipArchive;
echo "zip started.\n";
if ($zip->open($file, ZipArchive::CREATE) === TRUE) {
    foreach($array as $path){
        $zip->addFile($path, basename($path));
        echo "$path was added.\n";
    }
    $zip->close();
    echo 'done';
} else {
    echo 'failed';
}

?>

答案 1 :(得分:0)

将多个文件压缩为一个zip文件。

对于多个文件,您可以使用此代码压缩特定文件夹。

我已经尝试过这段代码并成功将多个文件压缩成一个zip文件。

我认为这有助于您的要求。

<?php

function Zip($source, $destination)
{

echo "called function";
    if (!extension_loaded('zip') || !file_exists($source)) {
        return false;
    }

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

    $source = str_replace('\\', '/', realpath($source));

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

        foreach ($files as $file)
        {
            $file = str_replace('\\', '/', $file);

            // Ignore "." and ".." folders
            if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..')) )
                continue;

            $file = realpath($file);

            if (is_dir($file) === true)
            {
                $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
            }
            else if (is_file($file) === true)
            {
                $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
            }
        }
    }
    else if (is_file($source) === true)
    {
        $zip->addFromString(basename($source), file_get_contents($source));
    }

    return $zip->close();
}

Zip('D:/tmp','D:/tmp/compressed.zip',true);


?> 

希望这有帮助