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正則表達(dá)式用法總結(jié)》、《PHP+ajax技巧與應(yīng)用小結(jié)》、《PHP運(yùn)算與運(yùn)算符用法總結(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)文章
PHP實現(xiàn)微信JS-SDK接口選擇相冊及拍照并上傳的方法
這篇文章主要介紹了PHP實現(xiàn)微信JS-SDK接口選擇相冊及拍照并上傳的方法,涉及php微信接口的調(diào)用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2016-12-12php批量轉(zhuǎn)換文件夾下所有文件編碼的函數(shù)類
分享一個php轉(zhuǎn)換文件夾下所有文件編碼函數(shù)類,適合發(fā)布網(wǎng)站的其他編碼版本,比如你有一個GBK版本 你想有一個UTF8版本 或者你只有GBK的源碼 你想二次開發(fā) 但是你不想改變IDE的編碼方式 你可以用這個程序?qū)⑵渑哭D(zhuǎn)化為UTF82017-08-08PHP自定義函數(shù)實現(xiàn)數(shù)組比較功能示例
這篇文章主要介紹了PHP自定義函數(shù)實現(xiàn)數(shù)組比較功能,涉及php針對數(shù)組的遍歷、比較、判斷等相關(guān)操作技巧,需要的朋友可以參考下2017-10-10