php如何把多个文件压缩成一个文件啊??求解,谢谢了!! 有人知道php如何把多个文件压缩成一个文件啊??求解,谢谢了!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在php.ini中打开zip扩展extension=php_zip.dll具体用法参考手册ZipArchive例子: $zip = new ZipArchive(); $res = $zip->open('test.zip', ZipArchive::CREATE); //不存在则创建 $filepath = 'abc.txt'; $zip->addFile($filepath); $zip->close(); 我以前在网上参考过的一份<?php /* @creates a compressed zip file 将多个文件压缩成一个zip文件的函数 * @$files 数组类型 实例array("1.jpg","2.jpg"); * @destination 目标文件的路径 如"c:/androidyue.zip" * @$overwrite 是否为覆盖与目标文件相同的文件 * @Recorded By Androidyue * @Blog:http://thinkblog.sinaapp.com */ function create_zip($files = array(),$destination = '',$overwrite = false) { //if the zip file already exists and overwrite is false, return false //如果zip文件已经存在并且设置为不重写返回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{ //如果没有真实有效的文件返回false return false; } } /**** //测试函数 $files=array('temp.php','test.php'); create_zip($files, 'myzipfile.zip', true); ****/ ?> php在进步!我们也要进步以前只能用代码完成的事情,现在用php扩展就可以了自 php5.3起,zip扩展已经内置了使用 ZipArchive 不再需要加载 php_zip(你也加载不到) .htaccess 域名指向 JAVASCRIPT执行不起来问什么 谁帮我看看我的建库建表代码有什么问题呗 ajax json的怪问题 输出图像 删去浏览器缓存的问题 PHP不乱码,数据库后台乱码 昨天说薪水太低要辞职,老板说了加薪,但是今天没动静,是立刻走还是等等看? 欢迎大家参观我新近搞好的论坛。 求个正则表达式 关于shell的一个问题求大神解决 利用phpmailer发送邮件时,显示不能连接到smtp服务器 LKOAAspcn.dll求助
例子: $zip = new ZipArchive();
$res = $zip->open('test.zip', ZipArchive::CREATE); //不存在则创建
$filepath = 'abc.txt';
$zip->addFile($filepath);
$zip->close();
/* @creates a compressed zip file 将多个文件压缩成一个zip文件的函数
* @$files 数组类型 实例array("1.jpg","2.jpg");
* @destination 目标文件的路径 如"c:/androidyue.zip"
* @$overwrite 是否为覆盖与目标文件相同的文件
* @Recorded By Androidyue
* @Blog:http://thinkblog.sinaapp.com
*/
function create_zip($files = array(),$destination = '',$overwrite = false) {
//if the zip file already exists and overwrite is false, return false
//如果zip文件已经存在并且设置为不重写返回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{
//如果没有真实有效的文件返回false
return false;
}
}
/****
//测试函数
$files=array('temp.php','test.php');
create_zip($files, 'myzipfile.zip', true);
****/
?>
以前只能用代码完成的事情,现在用php扩展就可以了自 php5.3起,zip扩展已经内置了
使用 ZipArchive 不再需要加载 php_zip(你也加载不到)