php ci框架超过内存,PHP ci框架封装的复制目录函数,报内存溢出

复制目录代码

/**

* [copyDir 复制文件夹]

* @param [type] $srcPath [来源目录]

* @param [type] $desPath [目的目录]

* @return [type] [description]

*/

public function copyDir($srcPath=null,$desPath=null){

$srcPath = isset($_POST['srcPath'])?$_POST['srcPath']:$srcPath;

$desPath = isset($_POST['desPath'])?$_POST['desPath']:$desPath;

if(!$srcPath && !$desPath){

exit;

}

$srcPath = $this->transChaset($srcPath,"GBK");

$desPath = $this->transChaset($desPath,"GBK");

header("Content-type: application/json");

if(!file_exists($desPath)){

if(mkdir($desPath,0777,true)){

}else{

$msg = '复制文件夹失败';

return json_encode(array("status"=>"error", "msg"=>$msg));

}

}

if($handle = opendir($srcPath)){

while (($item = readdir($handle)) !== false) {

$item = $this->transChaset($item,"GBK");

if($item!="."&&$item!=".."){

if(is_file($srcPath."/".$item)){

$srcPathItem = $srcPath."/".$item;

$desPathItem = $desPath."/".$item;

copy($srcPathItem,$desPathItem);

}

if(is_dir($srcPath."/".$item)){

$srcPathItem = $srcPath."/".$item;

$desPathItem = $desPath."/".$item;

/*$srcPathItem = $this->transChaset($srcPathItem,"UTF-8");

$desPathItem = $this->transChaset($desPathItem,"UTF-8");*/

$this->copyDir($srcPathItem,$desPathItem);

}

}

}

closedir($handle);

$data = '复制文件夹成功';

echo json_encode(array("status"=>"OK", "data"=>$data));

}else{

$msg = '复制文件夹失败';

return json_encode(array("status"=>"error", "msg"=>$msg));

}

}

转换字节码

/**

* [transChaset 转换字符类型]

* @param [type] $str [description]

* @param string $desCharacter [description]

* @return [type] [description]

*/

public function transChaset($str,$desCharacter = 'UTF-8'){

$code = mb_detect_encoding($str, array("ASCII","UTF-8","GB2312","GBK","EUC-CN","BIG5"));

$code = strtoupper($code);

if(($code == 'GB2312' || $code == 'GBK' || $code == 'UTF-8' || $code == 'EUC-CN' || $code == 'ASCII') && $code != $desCharacter){

$str = iconv($code,$desCharacter,$str);

}

return $str;

}

bVpIJ6

回复内容:

复制目录代码

/**

* [copyDir 复制文件夹]

* @param [type] $srcPath [来源目录]

* @param [type] $desPath [目的目录]

* @return [type] [description]

*/

public function copyDir($srcPath=null,$desPath=null){

$srcPath = isset($_POST['srcPath'])?$_POST['srcPath']:$srcPath;

$desPath = isset($_POST['desPath'])?$_POST['desPath']:$desPath;

if(!$srcPath && !$desPath){

exit;

}

$srcPath = $this->transChaset($srcPath,"GBK");

$desPath = $this->transChaset($desPath,"GBK");

header("Content-type: application/json");

if(!file_exists($desPath)){

if(mkdir($desPath,0777,true)){

}else{

$msg = '复制文件夹失败';

return json_encode(array("status"=>"error", "msg"=>$msg));

}

}

if($handle = opendir($srcPath)){

while (($item = readdir($handle)) !== false) {

$item = $this->transChaset($item,"GBK");

if($item!="."&&$item!=".."){

if(is_file($srcPath."/".$item)){

$srcPathItem = $srcPath."/".$item;

$desPathItem = $desPath."/".$item;

copy($srcPathItem,$desPathItem);

}

if(is_dir($srcPath."/".$item)){

$srcPathItem = $srcPath."/".$item;

$desPathItem = $desPath."/".$item;

/*$srcPathItem = $this->transChaset($srcPathItem,"UTF-8");

$desPathItem = $this->transChaset($desPathItem,"UTF-8");*/

$this->copyDir($srcPathItem,$desPathItem);

}

}

}

closedir($handle);

$data = '复制文件夹成功';

echo json_encode(array("status"=>"OK", "data"=>$data));

}else{

$msg = '复制文件夹失败';

return json_encode(array("status"=>"error", "msg"=>$msg));

}

}

转换字节码

/**

* [transChaset 转换字符类型]

* @param [type] $str [description]

* @param string $desCharacter [description]

* @return [type] [description]

*/

public function transChaset($str,$desCharacter = 'UTF-8'){

$code = mb_detect_encoding($str, array("ASCII","UTF-8","GB2312","GBK","EUC-CN","BIG5"));

$code = strtoupper($code);

if(($code == 'GB2312' || $code == 'GBK' || $code == 'UTF-8' || $code == 'EUC-CN' || $code == 'ASCII') && $code != $desCharacter){

$str = iconv($code,$desCharacter,$str);

}

return $str;

}

bVpIJ6

看了半天,原来是自己在函数开始的地方isset写了一个死循环,函数不停的在调用自身。

用html表单提交的方式看到不停的在输出$srcPath;

改成下面这种方式就可以了。

/**

* [copyDir 复制文件夹]

* @param [type] $srcPath [来源目录]

* @param [type] $desPath [目的目录]

* @return [type] [description]

*/

public function copyDir($srcPath=null,$desPath=null){

/*$srcPath = isset($_POST['srcPath'])?$_POST['srcPath']:$srcPath;

$desPath = isset($_POST['desPath'])?$_POST['desPath']:$desPath;*/

if(!$srcPath && !$desPath){

$srcPath = isset($_POST['srcPath'])?$_POST['srcPath']:null;

$desPath = isset($_POST['desPath'])?$_POST['desPath']:null;

}

if(!$srcPath && !$desPath){

exit;

}

$srcPath = $this->transChaset($srcPath,"GBK");

$desPath = $this->transChaset($desPath,"GBK");

if(!file_exists($desPath)){

mkdir($desPath,0777,true);

}

if($handle = opendir($srcPath)){

while (($item = readdir($handle)) !== false) {

if($item!="."&&$item!=".."){

$item = $this->transChaset($item,"GBK");

if(is_file($srcPath."/".$item)){

$srcPathItem = $srcPath."/".$item;

$desPathItem = $desPath."/".$item;

copy($srcPathItem,$desPathItem);

}

if(is_dir($srcPath."/".$item)){

$srcPathItem = $srcPath."/".$item;

$desPathItem = $desPath."/".$item;

/*$srcPathItem = $this->transChaset($srcPathItem,"UTF-8");

$desPathItem = $this->transChaset($desPathItem,"UTF-8");*/

$this->copyDir($srcPathItem,$desPathItem);

}

}

}

closedir($handle);

$data = '复制文件夹成功';

return json_encode(array("status"=>"OK", "data"=>$data));

}else{

$msg = '复制文件夹失败';

return json_encode(array("status"=>"error", "msg"=>$msg));

}

}

感谢 【CodeIgniter 中国1号】QQ群的帮助, 尤其感谢,曾经很牛,freda,我喂自己袋盐等的热心解答。

多用户的相册管理,需要建立相册表,别名,相册名,路径一一对应等。

这里的这个代码只是针对单一用户的相册操作,并不适用多用户相册管理。

看到楼主写的代码,当复制文件夹的时候,是不是只会复制文件夹,文件夹里面的文件会复制不过去呢?所以我感觉那点用上递归

if(is_dir($srcPath."/".$item)){

$func=__FUNCTION__;

$func=($srcPath."/".$item,$desPath."/".$item);

}

,我只是个菜鸟,如有错误,请指教.

相关标签:php ci

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值