如何在Laravel上上传视频

时间:2019-06-09 13:13:10

标签: laravel video-upload

我正在尝试将视频上传到未上传的laravel项目中,但是当我尝试上传图像时,它会完美运行

    {
        $request->validate([
            'file' => 'required',
        ]);
       $fileName = time();

       $request->file->move(public_path('videos'), $fileName);
       $fileupload = new FileUpload;
        $fileupload->filename=$fileName;
        $fileupload->save();

        return response()->json(['success'=>'File Uploaded Successfully']);
    }

1 个答案:

答案 0 :(得分:0)

好吧,我先存储文件,以便代码看起来像这样:

public function uploadVideo(Request $request) {
    $request->validate([
        'file' => 'required',
    ]);
    $filename = time();

    /* Storing the file on the disk  */
    $request->file->storeAs('videos', $filename);

    /* Recording the upload on the database */
    $fileupload = new FileUpload;
    $fileupload->filename = $filename;
    $fileupload->save();

    return response()->json(['success'=>'File Uploaded Successfully']);
}

实际上,在您的代码中,您正在移动的文件尚未存储...无法正常工作。

我建议您多读documentation about File Storage,因为您必须使用命令来建立符号链接:

php artisan storage:link