laravel 数据库表内的图片转传到阿里云oss

1. html+css+jquery

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>七牛转阿里</title>
    {!! Html::script('project/xundu/js/mobile/jquery-2.1.4.min.js') !!}
    <style>
        #t{
            margin: 0 auto;
            margin-top: 100px;
        }
        table tr td input{
            width: 400px;
            height: 50px;
            border: 2px solid #DEDEDE;
            font-size: 30px;
            border-radius: 10px;
            color: #00a2d4;
        }
        table tr td span{
            font-size: 30px;
            font-weight: 900;
            color: #5b6f7c;
        }
        #show{
            border: 2px solid #DEDEDE;
            width: 100%;
            height: 500px;
            border-radius: 10px;
            background-color: #9a9a9a;
            color: #fff;
            overflow:scroll;
        }
        table tr td:first-child{
            text-align: right;
        }
    </style>
</head>
<body>
<table id="t">
    <tr>
        <td></td>
        <td><span>上传OSS</span><br><br><br></td>
        <td></td>
    </tr>
    <tr>
        <td><span>表名:</span></td>
        <td><input type="text" name="database" id="database"></td>
        <td></td>
    </tr>
    <tr>
        <td><span>字段:</span></td>
        <td><input type="text" name="fidles" id="fidles"></td>
        <td></td>
    </tr>
    <tr>
        <td><span>初始ID</span></td>
        <td><input type="text" name="id" id="id"></td>
        <td></td>
    </tr>
    <tr>
        <td><span>数量:</span></td>
        <td><input type="text" name="num" id="num"></td>
        <td></td>
    </tr>
    <tr>
        <td></td>
        <td><input type="button" value="&nbsp;&nbsp;&nbsp;" οnclick="upload()"></td>
        <td></td>
    </tr>
</table>
<br>
<br>
<div id="show">

</div>
<script>
    function upload(){
        var database = $('#database').val();
        var fidles   = $('#fidles').val();
        var num      = $('#num').val();
        var id       = $('#id').val();
        $('#show').html('<span><p>正在查询...</p></span>');
        $.ajax({
            type: 'post',
            url: "{!! route('dsk_mall_photo.select') !!}",
            data: {'database':database,'fidles':fidles,'num':num,id:id},
            success:function(data){
                if(data){
                    $('#show').prepend('<span><p>查询成功!</p></span>');
                    console.log(data);
                    for(var i=0;i<data.length;i++) {
                        $.ajax({
                            type: 'post',
                            url: '{!! route('dsk_mall_photo.oss_sc') !!}',
                            data: {data:data[i],'database':database,'fidles':fidles},
                            success:function(data){
                                if(data){
                                    $('#show').prepend('<span><p>成功上传'+data['id']+''+data['thumb']+'</p></span>');
                                }
                            },
                            error:function(data){
                                console.log(data[i]);
                                $('#show').prepend('<span><p>上传失败!!</p></span>');
                            }
                        });
                    }
                    }else{
                    $('#show').prepend('<span><p>查询失败!</p></span>');
                    return;
                }
            },
            error:function(){
                $('#show').prepend('<span><p>查询失败!</p></span>');
                return;
            }
        });
    }
</script>
</body>
</html>

2. 后台

// 查询数据库中所有的图片
public function postSelect(Request $request){
    $database  = $request->input('database');
    $fidles    = $request->input('fidles');
    $num       = $request->input('num');
    $id        = $request->input('id');
    $key       = explode(',', $fidles);
    $key_id    = $key[0];
    $key_thumb = $key[1];
    $data = \DB::select('select ' . $fidles . ' from ' . $database . ' where id >= ' . $id . ' limit ' . $num);
    if(!$data) return  '查询失败';
    foreach($data as $k => $v){
        $arr[$k]['id']    = $v->$key_id;
        $arr[$k]['thumb'] = $v->$key_thumb;
    }
    return $arr;
}
// 上传
public function postOss_sc(Request $request){
    $database  = $request->input('database');
    $fidles    = $request->input('fidles');
    $key       = explode(',', $fidles);
    $key_thumb = $key[1];
    $data      = $request->input('data');
    $id        = $data['id'];
    $thumb     = $data['thumb'];
    if(strpos($thumb, 'xunduwang.oss-cn-beijing')) return ['id'=>$id, 'thumb'=>'上传失败,图片已在阿里云服务器上。'];
    if(empty($id)) return ['id'=>$id, 'thumb'=>'上传失败,id为空。'];
    if(empty($thumb)) return ['id'=>$id, 'thumb'=>'上传失败,图片为空。'];
    $a = file_get_contents($thumb);
    $imgName = date('YmdHis',time()).rand(10000,99999).".jpg";
    $img_path = 'image/'.$imgName;
    $resource = fopen(public_path().'/image/'.$imgName,'w+');
    fwrite($resource,$a);
    fclose($resource);
    // 上传阿里云
    $bucket       = Config::OSS_TEST_BUCKET;
    $ossClient    = Common::getOssClient();
    $object       = $img_path;
    $result       = $ossClient->UploadFile($bucket, $object, $img_path);
    if (!$result) {
        dump("上传图片失败");
    }
    unlink($object);
    $img = $result['oss-request-url'];
    \DB::update('update ' . $database . ' set ' . $key_thumb . '="' . $img . '" where id = ' . $id);
    if($data) return ['id'=>$id,'thumb'=>$img];

}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值