2015-03-31 52 views
0

我正在努力创建与ZipArchive的邮编。我修改了一个函数,让我从一组文件路径创建一个zip文件。ZipArchive - 邮编没有被创建,但不会引发错误

您是否可以发现此代码中的任何错误,防止创建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] =>) 
+0

要压缩特定文件夹? – RaMeSh 2015-03-31 10:37:59

+0

文件名中的空格? – pokeybit 2015-03-31 10:41:01

+0

@用户我将多个文件压缩到一个zip文件,但是zip文件没有被创建。 – michaelw90 2015-03-31 11:22:27

回答

2

我有一个动态数组值的另一个解决方案进入到拉链files.I已经测试了这是工作的罚款。

希望这有助于:

<?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'; 
} 

?> 
+0

感谢您的帮助@用户。终于得到它的工作:) – michaelw90 2015-04-07 09:09:56

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); 


?> 

希望这有助于

+0

评论不适用于扩展讨论;这个对话已经[转移到聊天](http://chat.stackoverflow.com/rooms/74198/discussion-on-answer-by-user-ziparchive-zip-is-not-getting-created-but-throws) 。 – Taryn 2015-03-31 14:09:47

相关问题