2013-03-11 57 views
-1

通过创建PHP ZIP我发现这个真棒代码通过此链接compress/archive folder using php script在一个特定的目录

<?php 
// Config Vars 

$sourcefolder = "./"   ; // Default: "./" 
$zipfilename = "myarchive.zip"; // Default: "myarchive.zip" 
$timeout  = 5000   ; // Default: 5000 

// instantate an iterator (before creating the zip archive, just 
// in case the zip file is created inside the source folder) 
// and traverse the directory to get the file list. 
$dirlist = new RecursiveDirectoryIterator($sourcefolder); 
$filelist = new RecursiveIteratorIterator($dirlist); 

// set script timeout value 
ini_set('max_execution_time', $timeout); 

// instantate object 
$zip = new ZipArchive(); 

// create and open the archive 
if ($zip->open("$zipfilename", ZipArchive::CREATE) !== TRUE) { 
    die ("Could not open archive"); 
} 

// add each file in the file list to the archive 
foreach ($filelist as $key=>$value) { 
    $zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key"); 
} 

// close the archive 
$zip->close(); 
echo "Archive ". $zipfilename . " created successfully."; 

// And provide download link ?> 
<a href="http:<?php echo $zipfilename;?>" target="_blank"> 
Download <?php echo $zipfilename?></a> 

我想创建一个特定目录的zip创建使用PHP的ZIP文件。

+0

您是否尝试过使用zip文件名的完整路径,而不是仅使用文件名? – 2013-03-11 21:44:42

+1

“我发现了这个非常棒的代码” - 您是否真的试图了解它的工作原理,或者只是希望我们在将它粘贴到您的应用中之前为您调整它? – Gordon 2013-03-11 21:45:36

+0

但是哥们,真棒 – AlienWebguy 2013-03-11 21:46:32

回答

3
$zipfilename = "/path/to/my/zip/folder/myarchive.zip"; 

只需确保该文件夹是writable

+0

它不起作用,因为下载链接将显示不正确,并会将我转移到未找到的页面或我的网站,而不是下载链接。 – 2013-03-11 21:48:58

+0

那么,当然,你需要添加下载链接的路径:) – AlienWebguy 2013-03-11 21:51:18

+0

该文件夹是可写的,因为它工作很多次 – 2013-03-11 21:54:12

相关问题