2015-08-25 28 views
0

我使用下面的函数来压缩文件,但在每个目录中它可以自动添加两个文件,如下所示(红色划线)。PHP Ziparchive主机上的Linux

如何在排除这些不需要的文件的同时压缩文件?

function ziparchive($name,$folder){ 
    // create object 
    $ziparchivename= $name.'.zip'; 
    //echo $ziparchivename; 
    $zip = new ZipArchive(); 

    // open archive 
    if ($zip->open($ziparchivename, ZIPARCHIVE::CREATE) !== TRUE) { 
    die ("Could not open archive"); 
    } 

    // initialize an iterator 
    // pass it the directory to be processed 
    $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($folder)); 

    // 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."; 
} 
+0

尝试在'foreach'迭代器中将返回模式目录('.'和'..')转义出来。 –

回答

0

一些像这样的事情应该工作。

$skipFiles = array('.', '..'); 

foreach ($iterator as $key=>$value) { 

if(!in_array($key, $skipFiles)){ 
    $zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key"); 
} 

php manual in_array

搜索的另一个地方是array_search。 另一个要搜索的地方是array_search。

+0

谢谢你,但它仍然没有改进 –

+0

检查$ key和$ value的值。确保你使用正确的。 –