Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload video with Laravel

I'm trying to upload a video while using Laravel. Though uploading images work fine for me when I change my controller line

echo '$file' . $file->getClientOriginalName() . '"/>';

to:

echo '<file src="uploads/' . $file->getClientOriginalName() . '"/>';

I only see uploaded written on a new page, but no video.

Controller:

<?php
namespace App\Http\Controllers;

use Illuminate\Support\Facades\Request;


class UploadController extends Controller
{

    public function upload(Request $request)
    {

        if(Request::hasFile('file')){

            echo 'Uploaded';
            $file = Request::file('file');
            $file->move('uploads', $file->getClientOriginalName());
            echo '$file' . $file->getClientOriginalName() . '"/>';
        }

    }
}

Routes:

Route::get('/', function () {
    return view('welcome');
});

Route::post('upload', 'UploadController@upload');

View:

<html>
    <head>
        <title>Laravel</title>

        <link href='//fonts.googleapis.com/css?family=Lato:100' rel='stylesheet' type='text/css'>

    </head>
    <body>
        <div class="container">
            <div class="content">

                <h1>File Upload</h1>
                <form action="{{ URL::to('upload') }}" method="post" enctype="multipart/form-data">
                    <label>Select image to upload:</label>
                    <input type="file" name="file" id="file">
                    <input type="submit" value="Upload" name="submit">
                    <input type="hidden" value="{{ csrf_token() }}" name="_token">
                </form>

            </div>
        </div>
    </body>
</html>
like image 298
Vish_TS Avatar asked Oct 19 '16 18:10

Vish_TS


People also ask

How can I upload large video files in laravel?

If you want to upload big files you should use streams. Here's the code to do it: $disk = Storage::disk('s3'); $disk->put($targetFile, fopen($sourceFile, 'r+')); PHP will only require a few MB of RAM even if you upload a file of several GB.


1 Answers

You can try like this:

Controller :

use Illuminate\Support\Facades\Request;


class UploadController extends Controller
{

    public function upload(Request $request)
    {

        if(Request::hasFile('file')){

            $file = Request::file('file');
            $filename = $file->getClientOriginalName();
            $path = public_path().'/uploads/';
            return $file->move($path, $filename);
        }

    }
}

php.ini files contains some limits that might affect this. Try changing these to high enough values:

upload_max_filesize = 10M
post_max_size = 10M
memory_limit = 32M
like image 198
Minhaj Mimo Avatar answered Oct 11 '22 14:10

Minhaj Mimo