欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

ThinkPHP怎么實(shí)現(xiàn)圖片上傳

這篇文章主要介紹“ThinkPHP怎么實(shí)現(xiàn)圖片上傳”,在日常操作中,相信很多人在ThinkPHP怎么實(shí)現(xiàn)圖片上傳問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”ThinkPHP怎么實(shí)現(xiàn)圖片上傳”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

成都創(chuàng)新互聯(lián)專注于企業(yè)營銷型網(wǎng)站、網(wǎng)站重做改版、萊州網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、成都h5網(wǎng)站建設(shè)、成都商城網(wǎng)站開發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為萊州等各大城市提供網(wǎng)站開發(fā)制作服務(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);
      //如果傳入成功就會(huì)將文件存儲(chǔ)路徑傳入數(shù)組$tmp[]之中
      if($res){
        $tmp[$k] = $res['savepath'].$res['savename'];
      }
    }
  }
  //將存儲(chǔ)傳入文件路徑的數(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>入臺(tái)證:</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();//生成申請(qǐng)時(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("添加失?。∽C件可能已存在");
}

其中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則每次都會(huì)重新編譯
  '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)添加以后,自然需要在后臺(tái)管理模塊上添加刪除的function

上面的顯示圖片的時(shí)候,用HTTP協(xié)議的絕對(duì)路徑拼接出來顯示圖片;

而刪除圖片則是,以入口文件index.php為準(zhǔn),是當(dāng)前文件夾下面的upload文件夾;

記住調(diào)用ThinkPHP之中的upload、uploadone方法返回來的只是上傳文件在upload文件夾下面的存儲(chǔ)位置、“'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']

是否大于零;

到此,關(guān)于“ThinkPHP怎么實(shí)現(xiàn)圖片上傳”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

文章題目:ThinkPHP怎么實(shí)現(xiàn)圖片上傳
標(biāo)題來源:http://chinadenli.net/article20/ppghjo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、網(wǎng)站內(nèi)鏈品牌網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站收錄商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計(jì)