ZipArchive-zip没有被创建,但没有抛出任何错误


ZipArchive - zip is not getting created, but throws no errors

我很难用ZipArchive创建zip。我修改了一个函数,让我从一组文件路径中创建一个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] => )

我有另一个将动态数组值输入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';
}
?>

将多个文件压缩为一个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);

?> 

希望这能帮助