laravel图片上传repo

316次阅读
没有评论

用来做laravel的图片上传的repo。然后我这个是把图片的位置参数传输到files表的location字段。然后,别的表调用files表的id来查找位置。

//start
<?php
namespace App\Repository;

use App\Models\Files;

class FileRepository{

    public function upload($request,$str)
    {

        $path = '';
        if ($request->$str)
        {
            $file=$request->file($str);
            if($file){
                //判断是否为合法文件
                if(@$file->isValid()){
                    //通过laravel自带的store方法进行文件的上传,其中第一个参数表示上传到哪个
                    //文件夹下,第二个参数为用哪个磁盘,也就是框架下面的filesystem.php里面的配置
                    $path=$file->store(date ('Ym'),'upload');
                }else{
                    $path = '';
                }
            }else
            {
                $path = '';
            }
        }
        if(!$path)
        {
            return '';
        }
        $file = new Files();
        $file->location = $path;
        $file->save();
        return $file->id;

    }
}
//over

 

 

李路昌
版权声明:本站原创文章,由 李路昌 2022-12-23发表,共计551字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)