>백엔드 개발 >PHP 튜토리얼 >PHP ci框架封装的复制目录函数,报内存溢出

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

WBOY
WBOY원래의
2016-06-06 20:28:501308검색

复制目录代码

<code>/**
     * [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));
        }
    }</code>

转换字节码

<code>/**
     * [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;
    }</code>

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

回复内容:

复制目录代码

<code>/**
     * [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));
        }
    }</code>

转换字节码

<code>/**
     * [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;
    }</code>

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

看了半天,原来是自己在函数开始的地方isset写了一个死循环,函数不停的在调用自身。
用html表单提交的方式看到不停的在输出$srcPath;
改成下面这种方式就可以了。

<code>/**
     * [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));
        }
    }</code>

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

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

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

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

<code>if(is_dir($srcPath."/".$item)){
    $func=__FUNCTION__;
    $func=($srcPath."/".$item,$desPath."/".$item);
}</code>

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

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.