简体   繁体   English

无法使用 PHP 和 Laravel 存储上传的图像

[英]Unable to store uploaded image using PHP and Laravel

I am learning Laravel and attempting to create a webpage to upload Picture albums.我正在学习 Laravel 并尝试创建一个网页来上传相册。 I have read up on the laravel documentation on verification and each field is being verified.我已经阅读了有关验证的 laravel 文档,每个字段都在验证中。 However, upon clicking submit, the image does not post to the store method.但是,单击提交后,图像不会发布到商店方法。

The route is set in web.php路由设置在 web.php

Route::get('/albums/create', 'AlbumsController@create')->name("album-create");
Route::post('/albums/store', 'AlbumsController@store')->name("album-store");

The form code is shown below:表单代码如下所示:

<div class="container">

  <h2> Create New Album </h2>

    <form method="post" action="{{ route('album-store') }}" enctype="multipart/form-data">
      <input type="hidden" name="_token" value="{{ csrf_token(@csrf) }}">

        <div class="form-group">
            <label for="name">Name</label>
            <input type="text" class="form-control" name="name" id="name" placeholder="Enter Name: ">
        </div>
        <div class="form-group">
            <label for="name">Description</label>
            <input type="text" class="form-control" name="description" id="description"  placeholder="Enter description">
        </div>
        <div class="form-group">
            <label for="name">Cover Image</label>
            <input type="file" class="form-control" name="cover_image" id="cover-image" placeholder="cover-image">
        </div>
        <button type="submit" class="btn btn-primary">Submit</button>
      </form>

and finally we have the controller function below:最后我们有下面的控制器功能:

public function store(Request $request) {
  $this->validate($request, [
    'name' => 'required',
    'description' => 'required',
    'cover-image' => 'required'
  ]);

  $filenameWithExtension = $request->file('cover-image')->getClientOriginalName();

      $filename = pathinfo($filenameWithExtension, PATHINFO_FILENAME);

      $extension = $request->file('cover-image')->getClientOriginalExtension();
// time stamping the images uplaoded
      $filenameToStore = $filename . '_' . time() . '.' . $extension;

// saving the file to the disk (can be found in 'storage/app/public/album_covers..')
      $request->file('cover-image')->storeAs('public/album_covers', $filenameToStore);

  $album = new Album();
  $album->name = $request->input('name');
  $album->description = $request->input('description');
  $album->cover_image = $filenameToStore;
  $album->save();

When I click submit on the form after filling the fields, I still receive the error "Cover-image" is required.当我在填写字段后单击表单上的提交时,我仍然收到错误消息“需要封面图片”。 enter image description here在此处输入图像描述

the field in html is cover_image while the validation is cover-image html中的字段是cover_image而验证是cover-image

<div class="form-group">
            <label for="cover-image">Cover Image</label>
            <input type="file" class="form-control" name="cover-image" id="cover-image" placeholder="cover-image">
        </div>

The labels for attribute is set to target the field with id="name", not related to the issue but i thought you need to look in the html file it needs some attention.属性的标签设置for以 id="name" 为目标的字段,与问题无关,但我认为您需要查看 html 文件,它需要一些注意。

your cover image input field name is cover_image and you take the value and validate it as cover-image write form group input filed like bellow您的封面图像输入字段名称是 cover_image 并且您获取该值并将其验证为封面图像写入表单组输入字段,如下所示

<div class="form-group"> <label for="name">Cover Image</label> <input type="file" class="form-control" name="cover-image" id="cover-image" placeholder="cover-image"> </div

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM