PHP CI framework encapsulated copy directory function, reported memory overflow

Source: Internet
Author: User
Tags html form

Copy directory code

/** * [copydir Copy folder] * @param [type] $srcPath [source directory] * @param [type] $desPath [destination directory] * @return [type] [Description] */Public Function copydir ($srcPath =null, $desPath =null) {$srcPath = Isset ($_post[' SRCP        Ath '])? $_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{$ms                g = ' Copy folder failed ';            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, $desPa                    Thitem);            }}} closedir ($handle);            $data = ' Copy folder succeeded ';        echo Json_encode (Array ("status" = "OK", "data" = $data));            }else{$msg = ' failed to copy folder ';        return Json_encode (Array ("Status" = "error", "msg" = = $msg)); }   } 

Convert byte 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;    }

Reply content:

Copy Directory Code

/** * [copydir Copy folder] * @param [type] $srcPath [source directory] * @param [type] $desPath [destination directory] * @return [type] [Description] */Public Function copydir ($srcPath =null, $desPath =null) {$srcPath = Isset ($_post[' SRCP        Ath '])? $_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{$ms                g = ' Copy folder failed ';            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, $desPa                    Thitem);            }}} closedir ($handle);            $data = ' Copy folder succeeded ';        echo Json_encode (Array ("status" = "OK", "data" = $data));            }else{$msg = ' failed to copy folder ';        return Json_encode (Array ("Status" = "error", "msg" = = $msg)); }   } 

Convert byte 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;    }

For a long while, the original is in the beginning of the function of the place Isset wrote a dead loop, function constantly in the call itself.
In the way of the HTML form submitted to see the output $srcpath;
Change it to the following way.

/** * [copydir Copy folder] * @param [type] $srcPath [source directory] * @param [type] $desPath [destination directory] * @return [type] [Description] */Public Function copydir ($srcPath =null, $desPath =null) {/* $srcPath = Isset ($_post[' SR        CPath '])? $_post[' Srcpath ']: $srcPath; $desPath = isset ($_post[' Despath ') "$_post[' Despath '): $desPath; */if (! $srcPath &&! $desPath) {$s            Rcpath = 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 ($ite M!= "." && $item! = "...") {$item = $thisTranschaset ($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, $desPa                    Thitem);            }}} closedir ($handle);            $data = ' Copy folder succeeded ';        return Json_encode (Array ("status" = "OK", "data" = $data));            }else{$msg = ' failed to copy folder ';        return Json_encode (Array ("Status" = "error", "msg" = = $msg)); }    }

Thanks to "CodeIgniter China 1th" QQ Group of help, especially thanks, once very cow, Freda, I feed myself bag salt and other enthusiastic answer.

Multi-user album management, need to create album table, alias, album name, path one by one correspondence and so on.

This code here is only for a single user album operation, not applicable to multi-user album management.

See the landlord write code, when copying folders, is not only copied folders, folders inside the file will be copied but go to it? So I feel like I'm using recursion.

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

, I'm just a rookie, if there is a mistake, please advise.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.