下面給大家介紹ThinkPHP怎么實(shí)現(xiàn)圖片上傳功能,希望對需要的朋友有所幫助!
直接上個(gè)例子,其中包括有單圖片文件上傳、多圖片文件上傳、以及刪除文件的一些操作、放置刪除數(shù)據(jù)庫的時(shí)候,僅僅刪除掉了數(shù)據(jù)庫之中的文件路徑、而不是一并刪除服務(wù)器之中的文件、放置服務(wù)器爆炸...
TP里面common文件夾里面function.php里面自定義方法:
<?php //文件上傳類(可以設(shè)置多個(gè)參數(shù)) function upload($file=null,$maxSize=0,$exts=0,$savePath='') { //調(diào)用 $upload = new \Think\Upload();// 實(shí)例化上傳類 $upload->maxSize = $maxSize;// 設(shè)置附件上傳大小 $upload->exts = $exts; //array('jpg', 'gif', 'png', 'jpeg'); 設(shè)置附件上傳類型 $upload->savePath = $savePath; // 設(shè)置附件上傳目錄 // 上傳文件 //如果單個(gè)文件還是多個(gè)文件 if($file){ $info = $upload->uploadOne($file); }else{ $info = $upload->upload(); } //判定是否文件上傳成功de if(!$info) { return false; }else{ // 上傳成功, return $info; } } //上傳圖片 function fab_upload($files ,$maxSize = 0,$exts = null,$savePath = '') { //判定文件信息是否為空 if(empty($files)){ return false; } if($exts === null){ $exts = array('jpg', 'gif', 'png', 'jpeg'); }else{ $exts = 0; } $tmp = array(); //將文件信息(數(shù)組)用foreach循環(huán)遍歷, foreach($files as $k => $v){ //判定文件大于0之后,將遍歷value作為參數(shù)傳入upload方法 if($v['size'] > 0){ $res = upload($v,$maxSize,$exts,$savePath); //如果傳入成功就會將文件存儲路徑傳入數(shù)組$tmp[]之中 if($res){ $tmp[$k] = $res['savepath'].$res['savename']; } } } //將存儲傳入文件路徑的數(shù)組return回去 return $tmp; } ?>
其實(shí)無論哪個(gè)文件上傳、都是需要用$_FILES變量區(qū)操控的、
上面的方法是fab_upload調(diào)用upload方法的;
在HTML上我們表單是醬紫寫的:
<form action="{:U('Index/infoupload')}" method="post"style="overflow: hidden;clear: both;" enctype="multipart/form-data"> <p class="contact_r col-md-4"> <label class="contact_rc contact_file"><span><b>入臺證:</b><input class="inp_zj1" type="file" name="rutaiimg" ></span></label> <!-- <a class="contact_sp fancybox" href="images/txz1.jpg" rel="external nofollow" rel="external nofollow" >如圖示</a> --> </p> <p class="contact_r col-md-4"> <label class="contact_rc contact_file"><span><b>通行證:</b><input class="inp_zj2" type="file" name="tongxingimg" ></span></label> <!-- <a class="contact_sp fancybox" href="images/txz1.jpg" rel="external nofollow" rel="external nofollow" >如圖示</a> --> </p> </form>
控制器之中如何處理上傳的文件(拼接路徑以及文件名、還有入庫失敗需要?jiǎng)h除文件,類似回調(diào))
/*調(diào)用寫好的方法進(jìn)行驗(yàn)證*/ $new_thumb = fab_upload($_FILES); // var_dump($new_thumb);die; $input['data']['addtime']=time();//生成申請時(shí)間 $input['data']['pretime']=strtotime($input['data']['pretime']);//將傳過來的日期轉(zhuǎn)換成時(shí)間戳 if($new_thumb && count($new_thumb) > 0){ $input['data'] = array_merge($input['data'],$new_thumb); } $f = $customer->add($input['data']); if($f){ $this->display('Index/infosuccess'); // $this->success("添加成功!",U('Index/infocheck',array('iccid'=>$input['data']['iccid']))); }else{//數(shù)據(jù)添加失敗即刪除照片 if($new_thumb){ $p = C('UNLINK_PATH').$new_thumb; unlink($p); } $this->error("添加失敗!證件可能已存在"); }
其中UNLINK_PATH變量在ThinkPHP之中的config文件里面定義、是路徑來的
<?php return array( 'DB_TYPE' => 'mysql', // 數(shù)據(jù)庫類型 'DB_HOST' => 'localhost', // 服務(wù)器地址 'DB_NAME' => 'urban', // 數(shù)據(jù)庫名 'DB_USER' => 'root', // 用戶名 'DB_PWD' => '123456', // 密碼 'DB_PORT' => 3306, // 端口 'DB_PREFIX' => 'fab_', // 數(shù)據(jù)庫表前綴 'DB_CHARSET'=> 'utf8', // 字符集 'CHECK_ROOT' => true, //開啟rbac權(quán)限 'TMPL_CACHE_ON' => false, // 是否開啟模板編譯緩存,設(shè)為false則每次都會重新編譯 'ACTION_CACHE_ON' => false, // 默認(rèn)關(guān)閉Action 緩存 'HTML_CACHE_ON' => false, // 默認(rèn)關(guān)閉靜態(tài)緩存 'FILE_PATH'=>'http://localhost/urban/Uploads/', 'WEB_PATH' => 'http://localhost/urban/index.php/', 'WEB_URL' => 'http://localhost/urban/', 'UNLINK_PATH' => './Uploads/', 'PWD_KEY' => 'jeiskAsdlLsdfqaiocvwphxzbtu', 'AUTO_LOGIN_TIME'=>3600 * 24 * 7, 'SHOW_PAGE_TRACE'=>true, //追蹤模式 'MY_CATCH_DIR' =>'./cache/', //緩存目錄 'CODE_PATH' =>'http://localhost/urban/fabp/phpqrcode/', // 存放二維碼的目錄 'qq_face' =>'http://localhost/urban/Public/site/images/arclist/', //qq表情路徑 'wxlogin' => array( 'appid' => 'wx35f5b9e9b90539ae', 'AppSecret' => '4de424bee1529a8abeda9c0c52aad3aa', 'callback' => 'http://localhost/urban/index.php/Home/Login/call_back.html' ), 'topic_pass'=>false, //是否開啟話題審核 );
當(dāng)添加以后,自然需要在后臺管理模塊上添加刪除的function
上面的顯示圖片的時(shí)候,用HTTP協(xié)議的絕對路徑拼接出來顯示圖片;
而刪除圖片則是,以入口文件index.php為準(zhǔn),是當(dāng)前文件夾下面的upload文件夾;
記住調(diào)用ThinkPHP之中的upload、uploadone方法返回來的只是上傳文件在upload文件夾下面的存儲位置、“'2016-09-02/57c94e71f0916.png'”(入庫也這個(gè)吧)
所以無論刪除還是顯示都需要用C方法拼接一下
if(IS_POST){ $input=I('post.'); $ids=implode(',',$input['id']); $brand=D('brand'); $img=$brand->where("brand_id in ($ids)")->getField('thumb',true); foreach($img as $v){ $p = C('UNLINK_PATH').$v; unlink($p); } $res=$brand->where("brand_id in ($ids)")->delete(); if($res){ $this->success("刪除運(yùn)營商品牌成功!"); }else{ $this->error("刪除運(yùn)營商品牌失敗!"); } }
之所以用了那個(gè)foreach;是因?yàn)閭鬟^來的id不是唯一一個(gè);是多選,刪除;
多選,并且傳過去相應(yīng)欄目ID的值是如何實(shí)現(xiàn)的呢
<foreach name="list" item="v"> <tr> <td class="center" width="80px"> <label> <input type="checkbox" class="ace" name="id[]" value="{$v.brand_id}"/> <span class="lbl"></span> </label> </td> <td>{$v.brand_name}</td> </tr> </foreach> <tr> <td colspan="2"> <button class="btn btn-xs btn-danger" onclick="return tijiao('del')"><i class="icon-trash bigger-110"></i>刪除</button> </td> </tr>
上面刪除的javascript方法是這樣寫的:
<script type="text/javascript"> function tijiao(type){ if(type == 'del'){ $('#my_form').attr('action',"{:U('Admin/Brand/brand_del')}"); }else if(type == 'sort'){ $('#my_form').attr('action',"{:U('Admin/Brand/brand_sort')}"); } return true; } </script>
附加:其實(shí)判定文件是否有上傳最好用這個(gè)數(shù)據(jù):
$_FILES['input_name']['size']
是否大于零;