如何使用laravel-admin删除图片

发布时间:2021-05-22 16:16:46 作者:Leah
来源:亿速云 阅读:421

这篇文章将为大家详细讲解有关如何使用laravel-admin删除图片,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

<?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)); //得到文件类型,并且都转化成小写
    $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);
    }
//开始移动文件到相应的文件夹
    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是默认显示的图片,uniquename是使用随机生成的文件名,deleteUrl是删除图片的路径

再在form方法后新建方法,删除数据库里的数据

  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);
  }

最后别忘记添加相应的路由:

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

Laravel 是什么

Laravel 是一套简洁、优雅的PHP Web开发框架。它可以让你从面条一样杂乱的代码中解脱出来;它可以帮你构建一个完美的网络APP,而且每行代码都可以简洁、富于表达力。

关于如何使用laravel-admin删除图片就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. php使用代码删除图片的方法
  2. 如何使用php实现上传图片的删除

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

laravel admin

上一篇:使用canvas怎么实现元素图片镜像翻转动画效果

下一篇:怎么在HTML5中实现Web Notification桌面右下角通知功能

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》