溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何使用laravel-admin刪除圖片

發(fā)布時間:2021-05-22 16:16:46 來源:億速云 閱讀:436 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)如何使用laravel-admin刪除圖片,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

<?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 是什么

Laravel 是一套簡潔、優(yōu)雅的PHP Web開發(fā)框架。它可以讓你從面條一樣雜亂的代碼中解脫出來;它可以幫你構(gòu)建一個完美的網(wǎng)絡(luò)APP,而且每行代碼都可以簡潔、富于表達(dá)力。

關(guān)于如何使用laravel-admin刪除圖片就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI