laravel基础之文件上传

本文介绍了如何在Laravel框架中实现文件上传操作,通过代码示例详细解析了上传过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用laravel进行文件上传十分简单。以下贴出部分代码



文件上传方法代码

//文件上传方法
    public function upload(Request $request) {

        if($request->isMethod('POST')){
//            var_dump($_FILES);
            $file = $request->file('source');

            //判断文件是否上传成功
            if($file->isValid()){
                //获取原文件名
                $originalName = $file->getClientOriginalName();
                //扩展名
                $ext = $file->getClientOriginalExtension();
                //文件类型
                $type = $file->getClientMimeType();
                //临时绝对路径
                $realPath = $file->getRealPath();

                $filename = date('Y-m-d-H-i-S').'-'.uniqid().'-'.$ext;

                $bool = Storage::disk('uploads')->put($filename, file_get_contents($realPath));

                var_dump($bool);
            }

            //dd($file);
            /**
            UploadedFile {#164 ▼
            -test: false
            -originalName: "填充文件.png"
            -mimeType: "image/png"
            -size: 10340
            -error: 0
            path: "D:\xampp\tmp"
            filename: "phpF0E0.tmp"
            basename: "phpF0E0.tmp"
            pathname: "D:\xampp\tmp\phpF0E0.tmp"
            extension: "tmp"
            realPath: "D:\xampp\tmp\phpF0E0.tmp"
            aTime: 2016-11-22 04:39:27
            mTime: 2016-11-22 04:39:27
            cTime: 2016-11-22 04:39:27
            inode: 0
            size: 10340
            perms: 0100666
            owner: 0
            group: 0
            type: "file"
            writable: true
            readable: true
            executable: false
            file: true
            dir: false
            link: false
            linkTarget: "D:\xampp\tmp\phpF0E0.tmp"
            }
             */
            exit;
        }

        return view('student.upload');
    }


//文件上传路由设置
Route::any('upload', 'StudentController@upload');

//        文件上传配置
        'uploads' => [
            'driver' => 'local',
            'root' => storage_path('app/uploads'),
        ],

文件上传view页面

@extends('layouts.app')

@section('content')
    <div class="container">
        <div class="row">
            <div class="col-md-8 col-md-offset-2">
                <div class="panel panel-default">
                    <div class="panel-heading">文件上传</div>
                    <div class="panel-body">
                        <form class="form-horizontal" role="form" method="POST" action="" enctype="multipart/form-data">
                            {{ csrf_field() }}


                            <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
                                <label for="file" class="col-md-4 control-label">请选择文件</label>

                                <div class="col-md-6">
                                    <input id="file" type="file" class="form-control" name="source">

                                    @if ($errors->has('password'))
                                        <span class="help-block">
                                        <strong>{{ $errors->first('password') }}</strong>
                                    </span>
                                    @endif
                                </div>
                            </div>

                            <div class="form-group">
                                <div class="col-md-6 col-md-offset-4">
                                    <button type="submit" class="btn btn-primary">
                                        <i class="fa fa-btn fa-sign-in"></i> 确认上传
                                    </button>


                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值