Thinkphp 更新圖片,刪除原圖,更新文字或者圖片互不干涉,可以只更新圖片或者只更新文字。

Alittile_seven發表於2017-09-22

好多新手同學有這個問題,更新圖片的時候原圖還在,這樣會導致伺服器的空間越來越小,這裡提供一個更新圖片的時候刪除原圖片;

還有一個問題例如:新聞頁面 有圖片有文字,想只更新圖片或者只更新文字,有時候你只更新文字它會提醒你沒有上傳檔案,這個文字在這這裡也會解決;

首先上程式碼

 public function update($id)
    {

 //預設顯示新增表單,這裡顯示更新頁面的顯示,沒有資料傳輸過來就顯示原來的圖文,通過id找到你更新的那個資料
        $id = intval($id);
        if (!IS_POST) {
            $model = M('msxl')->where("id= %d",$id)->find();
            $this->assign("category",getSortedCategory(M('msfl')->select()));
            $this->assign('msxl',$model);
            $this->display();
        }
       else {
            $model = D("Msxl");
            
//這裡是重點檢視是否有檔案上傳,你可以在上面列印一下dump($_FILES),這是一個二維陣列,如果有檔案上傳就會有提示你上傳檔案的資訊如下

array(1) {
  ["image"] => array(5) {
    ["name"] => string(5) "5.jpg"
    ["type"] => string(10) "image/jpeg"
    ["tmp_name"] => string(42) "C:\Users\PC\AppData\Local\Temp\php1262.tmp"
    ["error"] => int(0)
    ["size"] => int(142302)
  }
}
如果你沒有上傳檔案就會提示如下

array(1) {
  ["image"] => array(5) {
    ["name"] => string(0) ""
    ["type"] => string(0) ""
    ["tmp_name"] => string(0) ""
    ["error"] => int(4)
    ["size"] => int(0)
  }
}
這個時候通過$_FILES[image][size]就可以判斷是否有檔案上傳如果有程式往下走

            if($_FILES['image']['size'] != 0){
            //上傳單個影象
            $upload = new \Think\Upload();// 例項化上傳類
            $upload->maxSize   =     1*1024*1024 ;// 設定附件上傳大小
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 設定附件上傳型別
            $upload->rootPath  =      './Public/'; // 設定附件上傳根目錄
            $upload->savePath  =      'msupload/'; // 設定附件上傳(子)目錄
            $upload->saveName=array('uniqid','');//上傳檔案的儲存規則
            $upload->autoSub  = true;//自動使用子目錄儲存上傳檔案
            $upload->subName  = array('date','Ymd');
            // 上傳單個圖片
            $info   =   $upload->uploadOne($_FILES['image']);
            if(!$info) {// 上傳錯誤提示錯誤資訊
            $this->error($upload->getError());
            }else{// 上傳成功 獲取上傳檔案資訊
            $img_url=$info['savepath'].$info['savename'];
           
           
            $image  = new \Think\Image();
            $scrimg = $upload->rootPath.$img_url;
            $image -> open($scrimg );
            $image -> thumb(50,50);  //按照比例縮小
            $smallimg = $info['savepath']."small_".$info['savename'];
            $image -> save($upload->rootPath.$smallimg);
           
 //刪除原圖片這裡就是刪除原圖片的方法,如下兩個欄位的值是從前端頁面接收過來的,都是從資料庫讀取的原圖地址

$url = $data['pic'];
$smallurl = $data['picsmall'];

原圖地址都是隱藏域如下

<input type="file" name="image"  id="msxl-image" style="width:150px;" onchange="javascript:setImagePreview();">
<input type="hidden" name="pic" value="{$msxl.img_url}">
<input type="hidden" name="picsmall" value="{$msxl.imgsmall_url}">

這時候用到file_exists這個函式,判斷檔案是否存在,如果存在那麼就刪除,我這裡有一個數縮率圖


            $data = I();
            $url = $data['pic'];
            $smallurl = $data['picsmall'];
           
            $url = $_SERVER["DOCUMENT_ROOT"]."/Public/".$data['pic'];
            $smallurl = $_SERVER["DOCUMENT_ROOT"]."/Public/".$data['picsmall'];
            if (file_exists($url)) {
            unlink($url);
            if (file_exists($smallurl)) {
            unlink($smallurl);
            }
            }
           
             
           
           
            $data['img_url'] = $img_url;
            $data['time'] = time();
            $data['create_time'] = time();
            $data['user_id'] = 1;
            $data['imgsmall_url'] =$smallimg;
                     }  
            
                }else {
                $data = I();
                $img_url = $data['pic'];
                $smallimg = $data['picsmall'];
                $data['img_url'] = $img_url;
                $data['imgsmall_url'] =$smallimg;
                $data['time'] = time();
                $data['create_time'] = time();
                $data['user_id'] = 1;
               
                }
            
                if (!$model->data($data)->create()) {
                $this->error($model->getError());//輸出上傳錯誤資訊
                }else{
                if ($model->save($data)) {
                $this->success("更新成功", U('msxl/index'));
                } else {
                $this->error("更新失敗");
                }
                }
       }
    }    

這時候就達到我們需要的效果了。

相關文章