PHP Zip壓縮 在線對文件進行壓縮的函數(shù)
更新時間:2010年05月26日 00:31:29 作者:
PHP在線對文件進行Zip 壓縮函數(shù)代碼,用于使用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;
}
}
/***** Example Usage ***/
$files=array('file1.jpg', 'file2.jpg', 'file3.gif');
create_zip($files, 'myzipfile.zip', true);
PHP Zip 文件在線解壓縮的函數(shù)代碼
相關文章
PHP foreach遍歷多維數(shù)組實現(xiàn)方式
這篇文章主要為大家詳細介紹了PHP foreach遍歷多維數(shù)組實現(xiàn)方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11PHP下用rmdir實現(xiàn)刪除目錄的三種方法小結(jié)
PHP本身有一個rmdir()函數(shù)可以用來刪除目錄,不過要求必須是空目錄,本文列舉了三種方法。1、遞規(guī)法;2、系統(tǒng)調(diào)用法;3、循環(huán)法 。2008-04-04php 時間time與日期date之間的使用詳解及區(qū)別
PHP中有time函數(shù),也有date函數(shù),這兩個函數(shù)在使用時候的區(qū)別很明顯。但更應注意,time和date是兩個完全不時的格式,當然還有一種字符串格式。本文重點介紹這幾者的區(qū)別。2016-11-11