在 Laravel 项目中上传视频

问题描述 投票:0回答:2

当我尝试将视频/电影上传到我的网站时遇到问题。它不会将视频保存在数据库中,但它会在我的文件中创建一个文件夹“电影”,其中包含该视频,就像它应该做的那样。我还根据大小要求编辑了 php.ini 文件,并且我所做的会话表示它已上传。 这是我的代码

查看:

<div class="col-md-6">
     {!! Form::open(['method'=>'POST', 'action'=> 'MovieController@store', 'files' => true]) !!}
  <div class="form-group">
     {!! Form::label('movie_name', 'Enter Movie Name:') !!} <br>
     {!! Form::text('movie_name', null, ['class'=>'form-control'])!!}
  </div>
  <div class="form-group">
     {!! Form::label('uploaded_path', 'Select Movie:') !!} <br>
     {!! Form::file('uploaded_path', null, ['class'=>'form-control'])!!}
  </div>
</div>
<div class="form-group">
     {!! Form::label('actor_id', 'Actors:') !!}
     {!! Form::select('actor_id[]', $actors, null, ['class'=>'form-control js-example-basic-multiple', 'multiple' => 'multiple']) !!}
</div>
<div class="form-group">
     {!! Form::label('category_id', 'Category:') !!}
     {!! Form::select('category_id[]', $categories, null, ['class'=>'form-control js-example-basic-multiple', 'multiple' => 'multiple']) !!}
</div>

电影请求:

public function rules()
{
    return [
        'movie_name' => 'required|max:255',
        'uploaded_path' => 'mimetypes:video/avi,video/mpeg,video/mp4|required'
    ];
}

控制器:

public function store(MovieRequest $request)
{
    DB::beginTransaction();
    try {
        if ($request->hasFile('uploaded_path')) {
            $filenameWithExt = $request->file('uploaded_path')->getClientOriginalName();
            $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
            $extension = $request->file('uploaded_path')->getClientOriginalExtension();
            $fileNameToStore = $filename. '_'.time().'.'.$extension;
            $path = $request->file('uploaded_path')->storeAs('public/movies/', $fileNameToStore);
        } else {
            $fileNameToStore = 'novideo.mp4';
        }

        $movie = new Movie;
        $movie->movie_name = $request->input('movie_name');
        $movie->uploaded_path = $fileNameToStore;
        $movie->actors()->attach($request->input('actor_id'));
        $movie->categories()->attach($request->input('category_id'));
        $movie->save();

        DB::commit();
    } catch (\Exception $e) {
        DB::rollBack();
    }
    Session::flash('success', 'A movie was successfully UPLOADED in the database!');
    return redirect()->route('movies.index');
}
php database laravel upload
2个回答
0
投票

别忘了添加:

enctype="multipart/form-data"

去电影场。


0
投票

有时此错误是由 php.ini 引起的。文件大小超过2M,因为是视频文件。

转到 C:\xampp\php 目录并使用编辑器打开 php.ini。找到upload_max_filesizepost_max_size,并更改为100M。 然后重新启动 Apache 服务器,就可以了。

© www.soinside.com 2019 - 2024. All rights reserved.