Upload multimedia files in Laravel 5.7
Files uploading in any application is very useful tasks to upload multimedia files in Laravel 5.7 is a very easy task. first, we need to create a simple view form to upload a file and a new controller where we make logic code to upload file is Laravel.
In a view file, we need to create a form by adding the following line of code.
Form::open(array('url' => '/uploadfile','files'=>'true'));
When you add this line of code may be a chance to give an error so here is the solution click
Step 1: Create a view file called resource/view/upload.php
resource/view/upload.php
<html>
<body>
{!! Form::open(array('route' => 'files.upload', 'files' => true,'method'=>'post')) !!}
{{ Form::label('file', 'Upload New File:') }}
{{ Form::file('file', null, array('required' => '')) }}
{!! Form::submit( 'Upload Files', ['name' => 'upload', 'value' => 'upload'])!!}
{!! Form::close() !!}
</body>
</html>
Step 2 − Create a new controller called UploadFileController by executing the following command.
php artisan make:controller UploadFileController
After that add some code to upload a file in the folder.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class UploadFileController extends Controller {
public function index() {
return view('uploadfile');
}
public function showUploadFile(Request $request) {
$file = $request->file('image');
//Display File Name
echo 'File Name: '.$file->getClientOriginalName();
echo '<br>';
//Display File Extension
echo 'File Extension: '.$file->getClientOriginalExtension();
echo '<br>';
//Display File Real Path
echo 'File Real Path: '.$file->getRealPath();
echo '<br>';
//Display File Size
echo 'File Size: '.$file->getSize();
echo '<br>';
//Display File Mime Type
echo 'File Mime Type: '.$file->getMimeType();
//Move Uploaded File
$destinationPath = 'uploads';
$file->move($destinationPath,$file->getClientOriginalName());
}
}
Step 3: Add route in routes/web.php
Route::get('/uploadfile','[email protected]');
Route::post('/uploadfile','[email protected]')->name('files.upload');
Step 4: Enter this URL in the browser
//localhost:8000/uploadfile
You will result in some like this...
Brijpal Sharma
Hello, My Name is Brijpal Sharma. I am a Web Developer, Professional Blogger and Digital Marketer from India. I am the founder of Codermen. I started this blog to help web developers & bloggers by providing easy and best tutorials, articles and offers for web developers and bloggers...
1 Comments
Cloudi5
Publish on:-August 17th, 2020
Very interesting and it caught my attention. Bookmarking your article which will probably be my guide. Thank you very much.
by cloudi5
You must be logged in to post a comment.