[pho]將資料夾產生zip檔下載

回覆文章
布魯斯
Site Admin
文章: 211
註冊時間: 週四 2月 16, 2006 3:34 pm

[pho]將資料夾產生zip檔下載

文章 布魯斯 »

將createDirZip class放在CreateZipFile.inc.php中

代碼: 選擇全部

class createDirZip extends createZip {
 
	function get_files_from_folder($directory, $put_into) {
		if ($handle = opendir($directory)) {
			while (false !== ($file = readdir($handle))) {
				if (is_file($directory.$file)) {
					$fileContents = file_get_contents($directory.$file);
					$this->addFile($fileContents, $put_into.$file);
				} elseif ($file != '.' and $file != '..' and is_dir($directory.$file)) {
					$this->addDirectory($put_into.$file.'/');
					$this->get_files_from_folder($directory.$file.'/', $put_into.$file.'/');
				}
			}
		}
		closedir($handle);
	}
}
要壓縮成zip檔的資料夾,注意最後要有/

代碼: 選擇全部

$createZip = new createDirZip;
$createZip->addDirectory('themes/');
$createZip->get_files_from_folder('blog/wp-content/themes/', 'themes/');
產生zip檔下載

代碼: 選擇全部

$fileName = 'tmp/archive.zip';
$fd = fopen ($fileName, 'wb');
$out = fwrite ($fd, $createZip->getZippedfile());
fclose ($fd);
 
$createZip->forceDownload($fileName);
@unlink($fileName);
參考網址:
http://www.web-development-blog.com/arc ... n-the-fly/
http://olederer.users.phpclasses.org/pa ... nload.html
createzipfile-2010-03-13.zip
Class: Create ZIP File
(4.51 KiB) 已下載 29 次
回覆文章