Laravel 上传文件

版权声明:本人原创 https://blog.csdn.net/NYYandYF/article/details/88357248

1.路由 web.php

Route::post('uploading','UploadingController@uploading');

2.视图 uploading.blade.php

<!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>Document</title>
</head>
<body>
    <form action="{{url('uploading')}}" method="post" enctype="multipart/form-data">
        {{csrf_field()}}
        请选择图片:<input type="file" name="images">
        <input type='submit' value='上传'>
    </form>
</body>
</html>

3.在控制器 UploadingController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UploadingController extends Controller
{
    public function uploading(Request $r){
        $file = $r->file('image');    //获取文件所有信息
        if($file ->isValid()) { //判断文件是否存在
            $clientName = $file->getClientOriginalName();    //客户端文件名称..
            $entension = $file->getClientOriginalExtension();   //上传文件的后缀.
            $newName = md5(date('Ymdhis') . $clientName) . "." . $entension;    //定义      上传文件的新名称
            $path = $file->move('storage\uploads', $newName);    //把缓存文件移动到指定文件夹
        }
    print_r($path); //最终文件上传之后的地址
    }
}

猜你喜欢

转载自blog.csdn.net/NYYandYF/article/details/88357248
今日推荐