$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
所有的「神奇」,增加了文件在你CreateZipFile類,特別是zipDirectory()方法.... butyou似乎忘記了發佈這個。 – 2011-03-11 17:29:52
我沒有寫這個函數,我只是用這個類「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