Im currently doing the file upload. I searched through the internet regarding the file upload. The website that I saw is only saving through the images folder, i tried to put the Administrator::create(['image' => $request->image]); It saved to the database but its not the name of the image that i upload.. The name of the image after i-upload C:\\xampp\\tmp\\php306A.tmp
My Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Administrator;
class ImageController extends Controller
{
/**
* Create view file
*
* @return void
*/
public function imageUpload()
{
return view('image-upload');
}
/**
* Manage Post Request
*
* @return void
*/
public function imageUploadPost(Request $request)
{
$file = $request->file('image');
$this->validate($request, [
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$imageName = time().'.'.$request->image->getClientOriginalExtension();
$request->image->move(public_path('images'), $imageName);
// Administrator::create($request->all());
Administrator::create([
'image' => $request->image
]);
return back()
->with('success','Image Uploaded successfully.')
->with('path',$imageName);
}
}
?>
My View
<!DOCTYPE html>
<html>
<head>
<title>Laravel 5.3 Image Upload with Validation example</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="panel panel-primary">
<div class="panel-heading"><h2>Laravel 5.3 Image Upload with Validation example</h2></div>
<div class="panel-body">
@if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
@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>
<img src="/images/{{ Session::get('path') }}">
@endif
<form action="{{ url('image-upload') }}" enctype="multipart/form-data" method="POST">
{{ csrf_field() }}
<div class="row">
<div class="col-md-12">
<input type="file" name="image" />
</div>
<div class="col-md-12">
<button type="submit" class="btn btn-success">Upload</button>
</div>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
My Routes
Route::get('image-upload','ImageController@imageUpload');
Route::post('image-upload','ImageController@imageUploadPost');
My model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Administrator extends Model
{
protected $table = 'accounts';
protected $fillable = array(
'first_name',
'middle_name',
'last_name',
'email',
'image',
''
);
}
To get the uploaded image, you could change these lines:
$imageName = time().'.'.$request->image->getClientOriginalExtension();
$request->image->move(public_path('images'), $imageName);
to
$imageName = "";
if($request->hasFile('images')) {
$image = $request->file('images');
$imageName = time() . '.' . $image->getClientOriginalExtension();
$image->move(public_path('images'), $imageName);
}
And your create part:
Administrator::create([
'image' => $imageName
]);
view part
{!! Form::open(array('url'=>'insertfile','method'=>'POST' ,'class'=>'form-horizontal','files'=>true)) !!}
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label class="control-label col-sm-2" for="pwd">Upload:</label>
<div class="col-sm-4">
<input type="file" name="filenam" class="filename">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</div>
{!! Form::close() !!}
controller
public function insertFile(){
$file= Input::file('filenam');
$rules = array('file' => 'required|max:10000|mimes:jpeg,png,jpg,gif,svg,csv,xls,xlsx,doc,docx,pdf');
$validator = Validator::make(array('file'=> $file), $rules);
if($validator->passes()){
$destinationPath = 'up_file';
$filename = $file->getClientOriginalName();
$data=array(
'file_name' => $filename,
);
var_dump($data);
yourmodelname::insert($data);
$upload_success = $file->move($destinationPath, $filename);
}
}
in route:
Route::get('/uploadfile','UploadController@getView');
Route::post('/insertfile','UploadController@insertFile');
file will be upload in yourproject/public/up_file directory
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.