PHP ci架構封裝的複製目錄函式,報記憶體溢出

來源:互聯網
上載者:User

複製目錄代碼

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

,我只是個菜鳥,如有錯誤,請指教.

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.