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;
}
专业潜水员
9 years, 2 months ago
Answers
看了半天,原来是自己在函数开始的地方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,我喂自己袋盐等的热心解答。
多用户的相册管理,需要建立相册表,别名,相册名,路径一一对应等。
这里的这个代码只是针对单一用户的相册操作,并不适用多用户相册管理。
Astray
answered 9 years, 2 months ago