laravel图片上传

html页面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>表单</title>
</head>
<body>
<form action="{{url('file')}}" method="post" enctype="multipart/form-data">
    <table border="1" align="center">
        <tr>
            <td>昵称</td>
            <td><input type="text" name="name"/></td>
        </tr>
        <tr>
            <td>选择图片</td>
            <td><input type="file" name="photo"/></td>
        </tr>
        <tr>
            <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
            <td><input type="submit" value="提交"/></td>
            <td></td>
        </tr>
    </table>
</form>
</body>
</html>

php接值

 $file = Request::file('photo');
        $name = Request::input('name');
        $allowed_extensions = ["png", "jpg", "gif"];
        if ($file->getClientOriginalExtension() && !in_array($file->getClientOriginalExtension(), $allowed_extensions)) {
            return ['error' => 'You may only upload png, jpg or gif.'];
        }
        $destinationPath = 'storage/uploads/'; //public 文件夹下面建 storage/uploads 文件夹
        $extension = $file->getClientOriginalExtension();
        $fileName = str_random(10).'.'.$extension;
        $file->move($destinationPath, $fileName);
        $filePath = asset($destinationPath.$fileName);
       $info=DB::insert('insert into photo(pname,photo) VALUES (?,?)',[$name,$filePath]);
          if($info){
           return Redirect('/show');
          }else{
              echo "no";
          }

完毕

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Laravel上传图片并回显,你可以按照以下步骤进行操作: 1. 在表单中添加文件上传字段: ```html <form method="POST" action="/upload" enctype="multipart/form-data"> @csrf <input type="file" name="image"> <button type="submit">上传图片</button> </form> ``` 2. 在路由中定义上传图片的处理逻辑: ```php use Illuminate\Http\Request; Route::post('/upload', function (Request $request) { if ($request->hasFile('image')) { $path = $request->file('image')->store('public/images'); // 存储的路径为 "storage/images/filename" // 如果你想要访问图片,可以将路径存储到数据库,并使用 Storage::url() 获取完整路径 // 也可以直接拼接 URL,例如:$url = '/storage/images/' . $request->file('image')->hashName(); return redirect()->back()->with('success', '图片上传成功'); } return redirect()->back()->with('error', '请选择要上传的图片'); }); ``` 3. 在视图中回显上传的图片: ```html @if(session('success')) <div class="alert alert-success">{{ session('success') }}</div> @endif @if(session('error')) <div class="alert alert-danger">{{ session('error') }}</div> @endif @if(isset($url)) <img src="{{ $url }}" alt="Uploaded Image"> @endif ``` 上述代码中,`$url` 是存储图片路径的变量,可以在控制器中把它传递给视图。使用`$url`作为图片的`src`属性值,图片就会回显在页面上。 请确保在进行文件上传时,已经配置好了Laravel的文件存储系统,并且有合适的权限以保存和访问上传的图片。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值