2011-03-11 27 views
3
$fileToZip="License.txt"; 
$fileToZip1="CreateZipFileMac.inc.php"; 
$fileToZip2="CreateZipFile.inc.php"; 

$directoryToZip="../images"; // This will zip all the file(s) in this present working directory 

$outputDir="/"; //Replace "/" with the name of the desired output directory. 
$zipName="test.zip"; 

include_once("CreateZipFile.inc.php"); 
$createZipFile=new CreateZipFile; 


//Code toZip a directory and all its files/subdirectories 
$createZipFile->zipDirectory($directoryToZip,$outputDir); 

$rand=md5(microtime().rand(0,999999)); 
$zipName=$rand."_".$zipName; 
$fd=fopen($zipName, "wb"); 
$out=fwrite($fd,$createZipFile->getZippedfile()); 
fclose($fd); 
$createZipFile->forceDownload($zipName); 
@unlink($zipName); ` 

當我運行這段代碼,它會創建一個zip文件test.zip但是當我解壓,我會得到所有的文件夾images內的所有文件我不希望這種情況發生。我只想在解壓縮時獲取所有文件,而不是在文件夾中。我如何修改此代碼以實現它?有沒有其他方法可以做到這一點?謝謝。郵編使用目錄(而不是目錄)的PHP

+1

所有的「神奇」,增加了文件在你CreateZipFile類,特別是zipDirectory()方法.... butyou似乎忘記了發佈這個。 – 2011-03-11 17:29:52

+1

我沒有寫這個函數,我只是用這個類「CreateZipFile.inc.php」(http://www.phpclasses.org/browse/file/9524.html)。但我找到了解決我的問題的方法。使用'ZipArchive'類並替換'$ zip-> addFile($ file,$ file);'用這個'$ zip-> addFile($ file,pathinfo($ file,PATHINFO_BASENAME));' – Maggie 2011-03-11 17:46:18

回答

2

我認爲this是你在找什麼。 您應該能夠根據需要修改並修改它。這取決於ZipArchive類。

+0

喲,謝謝...它工作正常,當我取代'$ zip-> addFile($文件,$文件);'用這個'$ zip-> addFile($ file,pathinfo($ file,PATHINFO_BASENAME));' – Maggie 2011-03-11 17:47:09

0

這可能是一個長鏡頭,但你試圖改變這一點:

$directoryToZip="../images"; 

這樣:

$directoryToZip="../images/"; 
相關問題