php簡單創(chuàng)建zip壓縮文件的方法
本文實例講述了php簡單創(chuàng)建zip壓縮文件的方法。分享給大家供大家參考,具體如下:
/* creates a compressed zip file */
function create_zip($files = array(),$destination = '',$overwrite = false) {
//if the zip file already exists and overwrite is false, return false
if(file_exists($destination) && !$overwrite) { return false; }
//vars
$valid_files = array();
//if files were passed in...
if(is_array($files)) {
//cycle through each file
foreach($files as $file) {
//make sure the file exists
if(file_exists($file)) {
$valid_files[] = $file;
}
}
}
//if we have good files...
if(count($valid_files)) {
//create the archive
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
return false;
}
//add the files
foreach($valid_files as $file) {
$zip->addFile($file,$file);
}
//debug
//echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
//close the zip -- done!
$zip->close();
//check to make sure the file exists
return file_exists($destination);
}
else
{
return false;
}
}
使用方法:
$files_to_zip = array( 'preload-images/1.jpg', 'preload-images/2.jpg', 'preload-images/5.jpg', 'kwicks/ringo.gif', 'rod.jpg', 'reddit.gif' ); //if true, good; if false, zip creation failed $result = create_zip($files_to_zip,'my-archive.zip');
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP操作zip文件及壓縮技巧總結(jié)》、《php文件操作總結(jié)》、《php正則表達式用法總結(jié)》、《PHP+ajax技巧與應(yīng)用小結(jié)》、《PHP運算與運算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語法入門教程》、《php操作office文檔技巧總結(jié)(包括word,excel,access,ppt)》、《php日期與時間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
做了CDN獲取用戶真實IP的函數(shù)代碼(PHP與Asp設(shè)置方式)
asp取真實IP的代碼,搭環(huán)境測試無代理、一級或多級代理的情況,可以正常獲取2013-04-04
PHP流Streams、包裝器wrapper概念與用法實例詳解
這篇文章主要介紹了PHP流Streams、包裝器wrapper概念與用法,結(jié)合實例形式分析了php中流Streams與包裝器wrapper的基本概念及使用方法,需要的朋友可以參考下2017-11-11
windows服務(wù)器使用IIS時thinkphp搜索中文無效問題
在用ThinkPHP開發(fā)的網(wǎng)站,在linux服務(wù)器下使用過一段時間,一切正常。但是更換到windows服務(wù)器時,發(fā)現(xiàn)搜索的時候,無法搜索中文,查不出相應(yīng)的結(jié)果。查看數(shù)據(jù)庫發(fā)現(xiàn)數(shù)據(jù)是存在的。linux服務(wù)器下正常,而且搜索數(shù)字或字母程序正常,說明程序是沒有任何問題的。2023-06-06
php 連接mssql數(shù)據(jù)庫 初學(xué)php筆記
如果實現(xiàn)了PHP和MySQL鏈接了,PHP和MSSQL的鏈接其實很簡單; 支持MSSQL的本地鏈接和遠程鏈接2010-03-03
PHP實現(xiàn)二維數(shù)組中的查找算法小結(jié)
這篇文章主要介紹了PHP實現(xiàn)二維數(shù)組中的查找算法,涉及PHP數(shù)組遍歷、判斷、計算等相關(guān)操作技巧,需要的朋友可以參考下2018-06-06

