File Uploading in laravel framework?

View page

@if(count($errors)>0)
<div class="alert alert-danger">
<p>Upload Validation Error</p><br/><br/>
@foreach($errors->all() as $er)
<li>{{$er}}</li>
@endforeach
</div>
@endif

@if(session('success'))
<h3>{{session('success')}}</h3>
@endif


{{--
@if($message == Session::get('success'))
<div class="alert alert-success alert-block">

<button type="button" class="close" data-dismiss="alert">x</button>
<strong>{{ $message }}</strong>
</div>
<img src="/images/{{Session::get('path')}}" width="300" height="300"/>

@endif
--}}


<form name="frm" action="{{url('data_upload')}}" method="post" enctype="multipart/form-data">

@csrf


<div>
<input type="file" id="profileimage" name="profileimage">

</div>

<br/>

<button type="submit" class="btn btn-primary" style="float:right;">Submit</button>

</form>

File name : controller

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;

class UploadData extends Controller
{
public function index()
{
return view('imageupload');
}


public function uploadfile(Request $request)
{
$this->validate($request,['profileimage'=>'required|image|mimes:jpeg,png,jpg,gif|max:2048']);
$image = $request->file('profileimage');
$new_name = random_int(1,9999).'.'.$image->getClientOriginalExtension();
//$image->move(public_path('logo'),$new_name);

$uploaded_path = public_path('images');
$image->move($uploaded_path,$new_name);
return back()->with('success','images upload successfully')->with('image',$new_name);;
}


}

How to upload file in laravel

File name : index.php

<?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());
}
}

Route File

File name : index.php

Route::get('/uploadfile','UploadFileController@index');
Route::post('/uploadfile','UploadFileController@showUploadFile');

How to check File exist or not?

if(file_exists(public_path('images/mahi.jpg'))){
dd('File is exists.');
}else{
dd('File is not exists.');
}

if(File::exists(public_path('images/nusrat.jpg'))){
dd('File is exists.');
}else{
dd('File is not exists.');
}

How to check file exist or Not

File name : index.php

public function index()
{
if (Storage::exists(public_path('img/dummy.jpg'))) {
dd('File is Exists');
}else{
dd('File is Not Exists');
}
}





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here


Ittutorial