欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

laravel-admin的圖片刪除實例

 更新時間:2019年09月30日 16:37:38   作者:曉風(fēng)xy殘月  
今天小編就為大家分享一篇laravel-admin的圖片刪除實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

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

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

<?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);
    }
//開始移動文件到相應(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;$i<count($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是使用隨機生成的文件名,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的圖片刪除實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Laravel 5.4.36中session沒有保存成功問題的解決

    Laravel 5.4.36中session沒有保存成功問題的解決

    這篇文章主要給大家介紹了關(guān)于Laravel 5.4.36中session沒有保存成功問題的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-02-02
  • Laravel5.1 框架文件管理操作實例分析

    Laravel5.1 框架文件管理操作實例分析

    這篇文章主要介紹了Laravel5.1 框架文件管理操作,結(jié)合實例形式分析了laravel5.1框架文件管理相關(guān)的配置、磁盤獲取以及文件目錄操作技巧,需要的朋友可以參考下
    2020-01-01
  • 老生常談ThinkPHP中的行為擴展和插件(推薦)

    老生常談ThinkPHP中的行為擴展和插件(推薦)

    下面小編就為大家?guī)硪黄仙U凾hinkPHP中的行為擴展和插件(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • 在Laravel中使用MongoDB的方法示例

    在Laravel中使用MongoDB的方法示例

    這篇文章主要介紹了在Laravel中使用MongoDB的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • Yii2中cookie用法示例分析

    Yii2中cookie用法示例分析

    這篇文章主要介紹了Yii2中cookie用法,結(jié)合實例形式簡單分析了Yii2中cookie的設(shè)置、讀取、配置等相關(guān)操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2016-07-07
  • YII框架常用技巧總結(jié)

    YII框架常用技巧總結(jié)

    這篇文章主要介紹了YII框架常用技巧,結(jié)合實例形式總結(jié)分析了Yii框架控制器、查詢、表單驗證、SQL查詢等常用操作技巧與相關(guān)注意事項,需要的朋友可以參考下
    2019-04-04
  • laravel框架之?dāng)?shù)據(jù)庫查出來的對象實現(xiàn)轉(zhuǎn)化為數(shù)組

    laravel框架之?dāng)?shù)據(jù)庫查出來的對象實現(xiàn)轉(zhuǎn)化為數(shù)組

    今天小編就為大家分享一篇laravel框架之?dāng)?shù)據(jù)庫查出來的對象實現(xiàn)轉(zhuǎn)化為數(shù)組,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-10-10
  • PHP循環(huán)遍歷數(shù)組的3種方法list()、each()和while總結(jié)

    PHP循環(huán)遍歷數(shù)組的3種方法list()、each()和while總結(jié)

    這篇文章主要介紹了PHP循環(huán)遍歷數(shù)組的3種方法list()、each()和while總結(jié),本文重點在于對這3種方法的混合使用講解上,需要的朋友可以參考下
    2014-11-11
  • PHP使用PDO連接ACCESS數(shù)據(jù)庫

    PHP使用PDO連接ACCESS數(shù)據(jù)庫

    本文給大家分享的是一個簡單的php使用pdo方式連接access數(shù)據(jù)庫的方法,有需要的小伙伴可以參考下。
    2015-03-03
  • Laravel 實現(xiàn)添加多語言提示信息

    Laravel 實現(xiàn)添加多語言提示信息

    今天小編就為大家分享一篇Laravel 實現(xiàn)添加多語言提示信息,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-10-10

最新評論