php簡(jiǎn)單創(chuàng)建zip壓縮文件的方法
本文實(shí)例講述了php簡(jiǎn)單創(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正則表達(dá)式用法總結(jié)》、《PHP+ajax技巧與應(yīng)用小結(jié)》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語(yǔ)法入門教程》、《php操作office文檔技巧總結(jié)(包括word,excel,access,ppt)》、《php日期與時(shí)間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
php 中文字符入庫(kù)或顯示亂碼問(wèn)題的解決方法
這個(gè)的問(wèn)題就出在在php里沒(méi)有告訴mysql數(shù)據(jù)庫(kù)你要插入的數(shù)據(jù)是gbk類型的,要解決其實(shí)很簡(jiǎn)單。連接數(shù)據(jù)庫(kù)后加上這么一句話就OK了。2010-04-04PHP新手NOTICE錯(cuò)誤常見(jiàn)解決方法
PHP新手NOTICE錯(cuò)誤,特此寫給那些遇到和我一樣錯(cuò)誤的朋友。2011-12-12PHP實(shí)現(xiàn)隨機(jī)發(fā)放撲克牌
這篇文章主要為大家詳細(xì)介紹了PHP實(shí)現(xiàn)隨機(jī)發(fā)放撲克牌,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04PHP實(shí)現(xiàn)微信JS-SDK接口選擇相冊(cè)及拍照并上傳的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)微信JS-SDK接口選擇相冊(cè)及拍照并上傳的方法,涉及php微信接口的調(diào)用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12php批量轉(zhuǎn)換文件夾下所有文件編碼的函數(shù)類
分享一個(gè)php轉(zhuǎn)換文件夾下所有文件編碼函數(shù)類,適合發(fā)布網(wǎng)站的其他編碼版本,比如你有一個(gè)GBK版本 你想有一個(gè)UTF8版本 或者你只有GBK的源碼 你想二次開發(fā) 但是你不想改變IDE的編碼方式 你可以用這個(gè)程序?qū)⑵渑哭D(zhuǎn)化為UTF82017-08-08PHP自定義函數(shù)實(shí)現(xiàn)數(shù)組比較功能示例
這篇文章主要介紹了PHP自定義函數(shù)實(shí)現(xiàn)數(shù)組比較功能,涉及php針對(duì)數(shù)組的遍歷、比較、判斷等相關(guān)操作技巧,需要的朋友可以參考下2017-10-10