PHPci框架封装的复制目录函数,报内存溢出
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHPci框架封装的复制目录函数,报内存溢出,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5812字,纯文字阅读大概需要9分钟。
内容图文
![PHPci框架封装的复制目录函数,报内存溢出](/upload/InfoBanner/zyjiaocheng/240/d0fa3dd97d9a47ad8657a0394a732422.jpg)
复制目录代码
/**
* [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;
}
回复内容:
复制目录代码
/**
* [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;
}
看了半天,原来是自己在函数开始的地方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);
}
,我只是个菜鸟,如有错误,请指教.
内容总结
以上是互联网集市为您收集整理的PHPci框架封装的复制目录函数,报内存溢出全部内容,希望文章能够帮你解决PHPci框架封装的复制目录函数,报内存溢出所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。