Copy <! DOCTYPE html >
< html >
< head >
< title >Laravel Video Upload Form - ScratchCode.io</ title >
< link rel = "stylesheet" href = "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" >
</ head >
< body >
< div class = "container mt-5" >
< div class = "panel panel-primary" >
< div class = "panel-heading" >
< h2 >Laravel Video Upload Form- ScratchCode.io</ h2 >
</ div >
< div class = "panel-body" >
@if ($message = Session :: get ( 'success' ) )
< div class = "alert alert-success alert-block" >
< button type = "button" class = "close" data-dismiss = "alert" >×</ button >
< strong > {{ $message }} </ strong >
</ div >
@endif
@if ( count ( $errors ) > 0 )
< div class = "alert alert-danger" >
< strong >Whoops!</ strong > There were some problems with your input.
< ul >
@foreach ($errors -> all () as $error)
< li > {{ $error }} </ li >
@endforeach
</ ul >
</ div >
@endif
< form action = " {{ route ( 'store.video' ) }} " method = "POST" enctype = "multipart/form-data" >
@csrf
< div class = "row" >
< div class = "col-md-12" >
< div class = "col-md-6 form-group" >
< label >Title:</ label >
< input type = "text" name = "title" class = "form-control" />
</ div >
< div class = "col-md-6 form-group" >
< label >Select Video:</ label >
< input type = "file" name = "video" class = "form-control" />
</ div >
< div class = "col-md-6 form-group" >
< button type = "submit" class = "btn btn-success" >Save</ button >
</ div >
</ div >
</ div >
</ form >
</ div >
</ div >
</ div >
</ body >
</ html >
Copy <? php
namespace App \ Http \ Controllers ;
use App \ Models \ Video ;
use Illuminate \ Http \ Request ;
use Illuminate \ Support \ Facades \ Storage ;
class VideoController extends Controller
{
public function getVideoUploadForm ()
{
return view ( 'files.create' ) ;
}
public function uploadVideo ( Request $request)
{
$this -> validate ( $request , [
'title' => 'required|string|max:255' ,
'video' => 'required|file|mimes:jpg,png' ,
] ) ;
$fileName = $request -> video -> getClientOriginalName () ;
$filePath = 'videos/' . $fileName;
$isFileUploaded = Storage :: disk ( 'public' ) -> put ( $filePath , file_get_contents ( $request -> video )) ;
// File URL to access the video in frontend
$url = Storage :: disk ( 'public' ) -> url ( $filePath ) ;
if ($isFileUploaded) {
$video = new Video ();
$video -> title = $request -> title;
$video -> path = $filePath;
$video -> save () ;
return back ()
-> with ( 'success' , 'Video has been successfully uploaded.' ) ;
}
return back ()
-> with ( 'error' , 'Unexpected error occured' ) ;
}
}
C:\xampp8\htdocs\datvietcoconut\database\migrations\2023_02_06_152717_create_videos_table.php
Copy <? php
use Illuminate \ Database \ Migrations \ Migration ;
use Illuminate \ Database \ Schema \ Blueprint ;
use Illuminate \ Support \ Facades \ Schema ;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up ()
{
Schema :: create ( 'videos' , function ( Blueprint $table) {
$table -> id () ;
$table -> string ( 'title' ) ;
$table -> string ( 'path' ) ;
$table -> timestamps () ;
} ) ;
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down ()
{
Schema :: dropIfExists ( 'videos' ) ;
}
};
Copy <? php
namespace App \ Models ;
use Illuminate \ Database \ Eloquent \ Factories \ HasFactory ;
use Illuminate \ Database \ Eloquent \ Model ;
class Video extends Model
{
use HasFactory ;
protected $fillable = [
'title' , 'path'
];
}
Copy <? php
use Illuminate \ Support \ Facades \ Route ;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
use App \ Http \ Controllers \ VideoController ;
Route :: get ( 'video-upload' , [ VideoController ::class , 'getVideoUploadForm' ] ) -> name ( 'get.video' ) ;
Route :: post ( 'video-upload' , [ VideoController ::class , 'uploadVideo' ] ) -> name ( 'store.video' ) ;