Request->文件返回空值为什么?

时间:2019-09-27 11:55:39

标签: php html css laravel laravel-5

因此,我正在尝试创建各种视频存储,在其中可以查看电影的图像,标题,长度以及删除该图像的选项。问题是当我尝试上传图像时,它总是返回null。我也用Laravel 5.8.35

我尝试使用getClientOriginalExtension,但由于文件返回了null

,因此无法正常工作

Slika表示图像

Žanr的意思是流派

纳斯洛夫意为标题

Godina表示年份

Filmovi的意思是电影

Trajanje表示长度

Akcija表示动作

index.blade.php

<style>
    table,tr,td {
        border: 1px solid black;
    }

    table {
        border-collapse: collapse;
        width: 800px;
        font-size: 30px;
        font-weight: bold;
        text-align: center;
        align-self: center;
    }
</style>

<table>
    <tr style="background-color:lightgray">
        <td>Slika</td>
        <td>Naslov filma</td>
        <td>Godina</td>
        <td>Trajanje</td>
        <td>Akcija</td>
    </tr>
    @foreach($filmovi as $film)
    <tr>
    <td><img src="{{ $film->slika }}" alt="{{$film->slika}}"></td>
        <td>{{ $film->naslov }}</td>
        <td>{{$film->godina}}</td>
        <td>{{$film->trajanje}} minuta</td>
        <td> <a href="{{ route('filmovi.destroy', $film->id) }}" data-method="delete">[obriši]</a></td>

    </tr>
    @endforeach

    <div class="">
        <a href="{{ route('filmovi.create') }}" class="btn btn-primary">Dodaj novi film</a>
    </div>

create.blade.php

<div>

        <form action="{{ route('filmovi.store') }}" method="post">
 Naslov: <br>
 <input type="text" class="form-control" id="naslov" name="naslov">
 <br>
 Žanr: <div class="select">
    <select name="zanr" id="zanr">
        @foreach($zanr as $z )
    <option value="{{ $z->id }}">{{ $z->naziv }}</option>
        @endforeach
      </select>
</div>

  Godina: <div class="select" style="width:200px;">
    <select name="godina" id="godina">
         @for($i = (int)date("1900"); $i<=(int)date("Y"); $i++)
        <option value="{{ $i }}" name="godina" >{{ $i }}</option>
         @endfor
    </select>
  </div>

  Trajanje: <br>
  <input type="text" class="form-control" id="trajanje" name="trajanje">
  <br>

  Slika: <br>
  <form action="{{ route('filmovi.store') }}" method="post" enctype="multipart/form-data">
  <input type="file" name="slika" class="form-control" enctype="multipart/form-data">(max. 64 KB)
  <div class="form-group">
        <a href="{{ route('filmovi.index') }}" class="btn btn-primary">Natrag</a>
        <button type="submit" class="btn btn-success float-right">Objavi</button>
    </div>
  </form>
        </form>
 </div>

控制器

<?php

namespace App\Http\Controllers;

use App\Filmovi;
use App\Zanr;
use Illuminate\Http\Request;
use App\Traits\UploadTrait;

class FilmoviController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

     use UploadTrait;
    public function index()
    {
        $filmovi = Filmovi::all();

        return view('filmovi.index', compact('filmovi'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $zanr = Zanr::all();

        return view('filmovi.create', compact('zanr'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {


        $filmovi = Filmovi::create([
            'naslov'   => request('naslov'),
            'id_zanr'    => request('zanr'),
            'godina'  => request('godina'),
            'trajanje' => request('trajanje'),
            'slika' => request('slika'),
        ]);

        if($request->has('slika'))
    {
        dd($slike = $request->file("slika"));

        $dirpath = '/uploads/slike/';

        $path=$dirpath . $slike;

        $this->uploadOne($slike, $dirpath, 'public');

        $filmovi->slika = $path;

    }
    $filmovi->save();

        return redirect()->route('filmovi.index');    
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Filmovi  $filmovi
     * @return \Illuminate\Http\Response
     */
    public function show(Filmovi $filmovi)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Filmovi  $filmovi
     * @return \Illuminate\Http\Response
     */
    public function edit(Filmovi $filmovi)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Filmovi  $filmovi
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Filmovi $filmovi)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Filmovi  $filmovi
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $filmovi = Filmovi::find($id);
        $filmovi->delete();

    //   \Mail::to($post->user)->send(new PostDeleted($post));

        return redirect()->route('filmovi.index');
    }

}

UploadTrait

<?php 
namespace App\Traits;

use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;

trait UploadTrait
{
    public function uploadOne(UploadedFile $uploadedFile, $dirpath = null, $disk = 'public')
    {
        $file = $uploadedFile->storeAs($dirpath, $uploadedFile,$disk);

        return $file;
    }
}
?>

1 个答案:

答案 0 :(得分:0)

据我所知,有两种可能性: 1.以您的形式传递csrf-token 2.将enctype="multipart/form-data"添加到您的表单中。

此外,您应该将其保存到存储中并使用符号链接(使用命令php artisan storage:link)进行链接,以检查文档:https://laravel.com/docs/5.8/filesystem