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

php中文网
发布: 2016-06-06 20:28:50
原创
1606人浏览过

复制目录代码

/**
     * [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));
        }
    }
登录后复制

转换字节码

立即学习PHP免费学习笔记(深入)”;

/**
     * [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;
    }
登录后复制

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

Nanonets
Nanonets

基于AI的自学习OCR文档处理,自动捕获文档数据

Nanonets 258
查看详情 Nanonets

回复内容:

复制目录代码

/**
     * [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));
        }
    }
登录后复制

转换字节码

立即学习PHP免费学习笔记(深入)”;

/**
     * [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;
    }
登录后复制

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

看了半天,原来是自己在函数开始的地方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
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号