php zip 压缩 异常 压缩异常php递归exception 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 php压缩zip php 默认执行时间是30秒, 这个抛异常时, 不到5秒时间.ini文件中 memory_limit 设置成 512MB都没有用. 抛出的异常信息是什么?总得要据此判断问题的所在吧?我用你的程序压缩一个图片文件夹(包含 596 个文件,2 个文件夹)运行十余秒,并未发现任何异常另外在抛出异常时,应尽量带上 php 产生的错误信息 php错误配置为 error_reporting = E_ALL & ~E_NOTICE如果不用throw new Exception('添加文件失败,原始文件名:'.$path.'|新文件名:'.$fileinfo.$objZip->getStatusString() , '111');这种方式, 运行程序的话, 没有任何错误提示php版本是 5.2.3 在同事机器上的那个环境上运行, 文件夹有 420个文件, 能正常运行, 正常压缩 数据无丢失 .可能是环境配置问题, 具体是哪, 没有找出来. 如果不用 throw new Exception.... 就无任何错误提示那么你不是用了吗?又有什么提示呢? 是什么问题没有找到解决方法, 只能采用分部压缩了, 200个文件压缩一次提供代码 供参考<?php error_reporting(E_ALL); set_time_limit(0); header('Content-Type:text/html;charset=utf8'); $fileArr = array(); $basePath = dirname(__FILE__); $objZip = new ZipArchive(); if($objZip->open('test.zip', ZipArchive::CREATE)){ createZip($basePath ,$objZip, '' ,$fileArr); } $objZip->close(); if(empty($fileArr)) die('没有数据'); $index = 1; //一次压缩200个文件 try{ foreach ($fileArr as $key => $value){ if(($index-1)%200==0){ $objZip->open('test.zip', ZipArchive::CREATE); } $objZip->addFile($value['orgFile'],$value['newFile']); if($index % 200 == 0 ){ $objZip->close(); } } if($index % 200 != 0){ $objZip->close(); } }catch(Exception $e){ echo $e->getMessage(); } /** * 压缩文件, 不带绝对文件目录 * @param $basePath 要进行文件压缩的目录 * @param $objZip 压缩包对象 * @param $newDir 新目录,递归时用 */ function createZip($basePath, $objZip, $newDir='', &$fileArr=array()){ foreach (new DirectoryIterator($basePath) as $fileinfo){ $path = $basePath.DIRECTORY_SEPARATOR.$fileinfo; if($fileinfo != '.' && $fileinfo != '..'){ $fileinfo = ($newDir == '' ? $fileinfo : $newDir.DIRECTORY_SEPARATOR.$fileinfo); if(is_file($path)){ $fileArr[] = array('orgFile' => $path, 'newFile' => (string)$fileinfo); //echo 'aaa--'.$path.'<br>'; // if($objZip->addFile($path,$fileinfo)){// //echo 'aaa--'.$path.'<br>';// }else{// //throw new Exception('添加文件失败,原始文件名:'.$path.'|新文件名:'.$fileinfo.$objZip->getStatusString() , '111'); // } }else if(is_dir($path)){ if($objZip->addEmptyDir($fileinfo)){ //echo 'dir--'.$fileinfo.'<br>'; }else{ //throw new Exception('创建目录失败', '222'); } createZip($path,$objZip, $fileinfo, $fileArr); } } } } foreach 循环中 忘记把变量 $index递加了, 要不然, 每个文件都要打开ZIP包, 慢死 这样一个查询的PHP语句该怎么写出来?输出每天的日期和记录数 php 域名验证 正则替换 请教高手PHP增加和删除字段的办法 当点击“下载”时可以将我选中的歌曲下载下来,请问我该怎么做? 关于访问量统计程序 这JB代码到底想干嘛? 十万火急求救啊!php中上传视频文件不能播放问题! php与html表单的合并问题.. 微信提示redirect_uri 参数错误 如何用PHP做一个http的服务器上传下载文件? thinkphp的一个小问题
php 默认执行时间是30秒, 这个抛异常时, 不到5秒时间.
ini文件中 memory_limit 设置成 512MB都没有用.
总得要据此判断问题的所在吧?
我用你的程序压缩一个图片文件夹(包含 596 个文件,2 个文件夹)运行十余秒,并未发现任何异常
另外在抛出异常时,应尽量带上 php 产生的错误信息
如果不用throw new Exception('添加文件失败,原始文件名:'.$path.'|新文件名:'.$fileinfo.$objZip->getStatusString() , '111');
这种方式, 运行程序的话, 没有任何错误提示
php版本是 5.2.3 在同事机器上的那个环境上运行, 文件夹有 420个文件, 能正常运行, 正常压缩 数据无丢失 .
可能是环境配置问题, 具体是哪, 没有找出来.
那么你不是用了吗?又有什么提示呢?
提供代码 供参考
<?php
error_reporting(E_ALL);
set_time_limit(0);
header('Content-Type:text/html;charset=utf8');
$fileArr = array();
$basePath = dirname(__FILE__);
$objZip = new ZipArchive();
if($objZip->open('test.zip', ZipArchive::CREATE)){
createZip($basePath ,$objZip, '' ,$fileArr);
}
$objZip->close(); if(empty($fileArr)) die('没有数据');
$index = 1;
//一次压缩200个文件
try{
foreach ($fileArr as $key => $value){
if(($index-1)%200==0){
$objZip->open('test.zip', ZipArchive::CREATE);
}
$objZip->addFile($value['orgFile'],$value['newFile']);
if($index % 200 == 0 ){
$objZip->close();
}
}
if($index % 200 != 0){
$objZip->close();
}
}catch(Exception $e){
echo $e->getMessage();
}
/**
* 压缩文件, 不带绝对文件目录
* @param $basePath 要进行文件压缩的目录
* @param $objZip 压缩包对象
* @param $newDir 新目录,递归时用
*/
function createZip($basePath, $objZip, $newDir='', &$fileArr=array()){
foreach (new DirectoryIterator($basePath) as $fileinfo){
$path = $basePath.DIRECTORY_SEPARATOR.$fileinfo;
if($fileinfo != '.' && $fileinfo != '..'){
$fileinfo = ($newDir == '' ? $fileinfo : $newDir.DIRECTORY_SEPARATOR.$fileinfo);
if(is_file($path)){
$fileArr[] = array('orgFile' => $path, 'newFile' => (string)$fileinfo);
//echo 'aaa--'.$path.'<br>';
// if($objZip->addFile($path,$fileinfo)){
// //echo 'aaa--'.$path.'<br>';
// }else{
// //throw new Exception('添加文件失败,原始文件名:'.$path.'|新文件名:'.$fileinfo.$objZip->getStatusString() , '111');
// }
}else if(is_dir($path)){
if($objZip->addEmptyDir($fileinfo)){
//echo 'dir--'.$fileinfo.'<br>';
}else{
//throw new Exception('创建目录失败', '222');
}
createZip($path,$objZip, $fileinfo, $fileArr);
}
}
}
}