2011-04-15 87 views
1

我想要的代碼,使樹中的所有文件在根ZipArchive .zip文件顯示所有文件在根

<?PHP 
// create object 
$zip = new ZipArchive(); 

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

// list of files to add 
// list of files to add 
$fileList = array(
    'im/asd.pdf', 
    'im/df.pdf', 
    'im/d/qoyyum.txt' 
); 

// add files 
foreach ($fileList as $f) { 
    $zip->addFile($f) or die ("ERROR: Could not add file: $f"); 
} 

// close and save archive 
$zip->close(); 
echo "Archive created successfully.";  
?> 

例如文件夾.zip文件/ IM含有

/im/asd.pdf 
/im/df.pdf 
/im/d/qoyyum.txt 

的「my-archive.zip」提取物應該是這樣的

my-archive/asd.pdf 
my-archive/df.pdf 
my-archive/qoyyum.txt 

我想阻止的文件夾層次提取拉鍊時。讓每一個文件都應該在提取的ZIP文件夾的根

請建議小費做

回答

2

有一個稱爲localname(見here),它可以讓你設置文件的本地名稱在ZipArchive::addFile()第二個參數在zip檔案中。使用它來覆蓋默認的目錄結構。

foreach ($fileList as $f) { 
    $filename_parts = explode('/', $f); // Split the filename up by the '/' character 
    $zip->addFile($f, end($filename_parts)) or die ("ERROR: Could not add file: $f"); 
} 
+0

謝謝,它的工作 – 2011-04-15 09:50:02

+0

偉大的解決方案,jackbot。 – 2bard 2011-04-15 11:14:23