laravel-admin的图片删除实例

2025-05-29 0 74

对laravel-admin的图片上传机制有深深的疑惑,在用户信息页面上删除头像图片就会报错,当时用的是1.4的,后来更新1.5 发现删除按钮直接消失了,在使用过程中,要是在form中正常使用image就好用,稍微写的复杂一点(比如我把$form->image写在tab里的时候)就不好用了。

针对这个问题写了一个方法,(也不知道适不适用哈)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89
<?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中这么写:

?

1

2
$form->image('img','图片')->deleteUrl(admin_url('mconfig/deleteUrl/' . img))->uniqueName()->value('1.jpg');

//其中value是默认显示的图片,uniquename是使用随机生成的文件名,deleteUrl是删除图片的路径

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

?

1

2

3

4

5

6

7

8

9

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

}

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

?

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

以上这篇laravel-admin图片删除实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。

原文链接:https://blog.csdn.net/qq_33541033/article/details/78709576

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 laravel-admin的图片删除实例 https://www.kuaiidc.com/91978.html

相关文章

发表评论
暂无评论