PHP 文件/圖片上傳

簡單的文件上傳方法,根據自己的需求做改動即可
if($this->request->data){
	$data = $this->request->data;
	//圖片上傳
	$file = $_FILES;
	if(!empty($file['img'])){
	    if(is_uploaded_file($_FILES['img']['tmp_name'])){
	        global $gFilePath; //上傳文件文件地址
	        $gFilePath = $gFilePath;
	        $path = $gFilePath.'chanpin/'.date("Ym").'/';
	        if (!file_exists($path)){ //判斷是否存在文件路徑,不存在則創建
              	mkdir($path, 0777, true);
               	chmod($path,0777);
            }
	        $ext = pathinfo($_FILES['img']['name'] , PATHINFO_EXTENSION);
	        $new_name = $path.uniqid(). '.' . $ext;
	        if(false != (move_uploaded_file($_FILES['img']['tmp_name'], $new_name))){
	            $data['img'] = str_replace($GLOBALS['gFileSave'], '', $new_name);
	        }
	    }
	}
	$this->Chanpin->useTable='chanpin'; //使用表   
	$rel = $this->Chanpin->save($data); //插入或更新表
	if ($rel) { //保存成功或失敗判斷
	    $this->Session->setFlash("操作成功!");  
	    $this->redirect("../Chanpin/lists?p=".$data['p']);          
	}else{
	    $this->Session->setFlash("操作失敗!");            
	}

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章