濮阳杆衣贸易有限公司

主頁 > 知識(shí)庫 > laravel-admin的圖片刪除實(shí)例

laravel-admin的圖片刪除實(shí)例

熱門標(biāo)簽:德陽中江如何申請400開頭電話 青白江地圖標(biāo)注 聊城電話外呼系統(tǒng)公司 辦理重慶400電話 江蘇電商外呼系統(tǒng)運(yùn)營商 沛縣400電話辦理 智能電話機(jī)器人好公司門薩維 銅川電話機(jī)器人價(jià)格 AI電話機(jī)器人OEM貼牌

對laravel-admin的圖片上傳機(jī)制有深深的疑惑,在用戶信息頁面上刪除頭像圖片就會(huì)報(bào)錯(cuò),當(dāng)時(shí)用的是1.4的,后來更新1.5 發(fā)現(xiàn)刪除按鈕直接消失了,在使用過程中,要是在form中正常使用image就好用,稍微寫的復(fù)雜一點(diǎn)(比如我把$form->image寫在tab里的時(shí)候)就不好用了。

針對這個(gè)問題寫了一個(gè)方法,(也不知道適不適用哈)

?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Form\Field\File;
use Illuminate\Http\UploadedFile;

class FileController extends Controller
{
  use ModelForm;

  public function index($type,$file=null,$ajax=true,$file_name="")
  {
    $file = $file ? $file : $_FILES['img'];

    if($file['error']!=0){
      $data = array('status'=>false,'msg'=>trans('admin::lang.Upload_error'));
      return $ajax ? json_encode($data) : $data;
    }


    //得到文件名稱
    $name = $file['name'];
    $img_type = strtolower(substr($name,strrpos($name,'.')+1)); //得到文件類型,并且都轉(zhuǎn)化成小寫
    $allow_type = array('jpg','jpeg','gif','png'); //定義允許上傳的類型
//判斷文件類型是否被允許上傳
    if(!in_array($img_type, $allow_type)){
      $data = array('status'=>false,'msg'=>trans('admin::lang.imgtype_error').$img_type);
      return $ajax ? json_encode($data) : $data;
    }
//判斷是否是通過HTTP POST上傳的
    if(!is_uploaded_file($file['tmp_name'])){
      $data = array('status'=>false,'msg'=>trans('admin::lang.post_img'));
      return $ajax ? json_encode($data) : $data;
    }
    $file_name = $file_name ? $file_name.'.'.$img_type : md5(uniqid()).Carbon::now()->timestamp.'.'.$img_type;

    if($type=='attr_img'){
      $upload_path = public_path().'/upload/goods/attr_img/'; //上傳文件的存放路徑
      $path = "goods/attr_img/";
    }elseif($type=='goods'){
      $upload_path = public_path().'/upload/goods/'; //上傳文件的存放路徑
      $path = "goods/";
    }else{
      $upload_path = public_path().'/upload/'.$type.'/'; //上傳文件的存放路徑
      $path = $type."/";
    }
    if(!is_dir($upload_path)){
      @mkdir($upload_path);
    }
//開始移動(dòng)文件到相應(yīng)的文件夾
    if(move_uploaded_file($file['tmp_name'],$upload_path.$file_name)){
      $data['status'] = true;
      $data['path'] = $path.$file_name;
      $data['view_path'] = config('admin.upload.host').$path.$file_name;
    }else{
      $data = array('status'=>false,'msg'=>trans('admin::lang.moveimg_error'));
      return $ajax ? json_encode($data) : $data;
    }
    if($ajax){
      return json_encode($data);
    }else{
      return $data;
    }
  }

  public function multipleImg($type,$files,$ajax=true){
    $imgs = array('status'=>true);
    for($i=0;$icount($files['name']);$i++){
      $file['name'] = $files['name'][$i];
      $file['type'] = $files['type'][$i];
      $file['tmp_name'] = $files['tmp_name'][$i];
      $file['error'] = $files['error'][$i];
      $file['size'] = $files['size'][$i];
      $data = $this->index($type,$file,false);
      if($data['status']){
        $imgs['path'][$i] = $data['path'];
        $imgs['view_path'][$i] = $data['view_path'];
      }else{

        return $ajax ? json_encode(array('status'=>false,'msg'=>$data['msg'])) : array('status'=>false,'msg'=>$data['msg']);
      }
    }
    return $ajax ? json_encode($imgs) : $imgs;
  }
}

然后在form中這么寫:

 $form->image('img','圖片')->deleteUrl(admin_url('mconfig/deleteUrl/' . img))->uniqueName()->value('1.jpg');
 //其中value是默認(rèn)顯示的圖片,uniquename是使用隨機(jī)生成的文件名,deleteUrl是刪除圖片的路徑

再在form方法后新建方法,刪除數(shù)據(jù)庫里的數(shù)據(jù)

  public function deleteUrl($img){
    $mconfig = MConfigModel::where('img',$img)->first();
    $path = config('admin.upload.host').$mconfig->val;
    if(file_exists($path)){
      @unlink ($path);
    }
    $mconfig->val = "";
    $mconfig->save();
    return array('status'=>true);
  }

最后別忘記添加相應(yīng)的路由:

$router->put('/mconfig/deleteUrl/{img}','MConfigController@deleteUrl');

以上這篇laravel-admin的圖片刪除實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • laravel實(shí)現(xiàn)上傳圖片的兩種方式小結(jié)
  • 利用laravel+ajax實(shí)現(xiàn)文件上傳功能方法示例
  • 在laravel5.2中實(shí)現(xiàn)點(diǎn)擊用戶頭像更改頭像的方法

標(biāo)簽:赤峰 鷹潭 烏魯木齊 迪慶 濟(jì)寧 山南 三亞 南寧

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《laravel-admin的圖片刪除實(shí)例》,本文關(guān)鍵詞  laravel-admin,的,圖片,刪除,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《laravel-admin的圖片刪除實(shí)例》相關(guān)的同類信息!
  • 本頁收集關(guān)于laravel-admin的圖片刪除實(shí)例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    北票市| 阜阳市| 石泉县| 兴义市| 东源县| 泉州市| 翼城县| 蒙山县| 五寨县| 枣强县| 东兴市| 文化| 修文县| 涡阳县| 绥宁县| 来凤县| 乐亭县| 抚远县| 板桥市| 临夏县| 大悟县| 乌鲁木齐县| 遂昌县| 枣庄市| 日照市| 全椒县| 琼结县| 郧西县| 桃园县| 镇巴县| 阿尔山市| 关岭| 太原市| 濮阳市| 江门市| 中江县| 柳州市| 巍山| 绥德县| 缙云县| 什邡市|