複製目錄代碼
/** * [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);}
,我只是個菜鳥,如有錯誤,請指教.