Laravel 5.6 upload image - php

I want to upload images with laravel:I created the field in mysql:
imagepath varchar 250
Created the form:
<form method="post" id="formId" action="/ticket" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group ">...other fields
<div class="form-group">
<input type="file" class="form-control-file" name="imageFile" id="imageFile" aria-describedby="fileHelp">
</div>
In my controller:
public function store(Request $request)
{
$fileName = request()->imageFile->getClientOriginalExtension();
dd($fileName);//echos ".jpg"
$tickes = Users::create(['imagePath' => $fileName]);
}
However my db is always null!I want to store it in my public/storage folder in Laravel.
Please help!

you can use this code for upload image to `public/storage/ directory :
if ($request->hasFile('input_img')) {
if($request->file('input_img')->isValid()) {
try {
$file = $request->file('input_img');
$name = rand(11111, 99999) . '.' . $file->getClientOriginalExtension();
# save to DB
$tickes = Users::create(['imagePath' => 'storage/'.$name]);
$request->file('input_img')->move("storage", $name);
} catch (Illuminate\Filesystem\FileNotFoundException $e) {
}
}
}

This code is only saving the file extension, not the image path.
You need to save the file to storage before your database can get a path to it.
Laravel has really good documentation and this link will help you figure this out. https://laravel.com/docs/5.6/filesystem

Related

File not uploading

I was working on a project and was trying to develop a file uploading system for skins.
When I tried to upload my skin, I was given "Call to a member function storeAs() on null"
public function uploadSkin(Request $request)
{
/* $request->validate([
'skins' => 'required|mimes:png|max:1024',
]); */
$storage_dir = storage_path('app/skins');
$request->file('skins')->storeAs($storage_dir, Auth::user->name . '.png');
return route('settings')->with('success', 'skin uploaded :)');
}
Form code:
<form method="post" enctype="multipart/form-data" action="/settings">
#csrf
<br/>
<div class="form-group">
<input type="file" class="form-control-file" id="skins" name="skins" required>
</div>
<button type="submit" class="btn btn-success">Upload</button>
</form>
To store a file like an image or any kind of files you can use a code like this:
public function uploadSkin(Request $request){
$image = $request->file('skins');
if ($image != null) {
$image->move('uploads/skins/', Auth::user()->name . $image->getClientOriginalExtension());
}
return route('settings')->with('success', 'skin uploaded :)');
}
To uppload a file there are various ways in the laravel but for now you can try this to simply move your file to your directory:
if($files= $request->file('skins')){
$files->move('uploads/skins/', Auth::user->name . '.png');
}

How can I upload profile pic in laravel?

In add employee form I have to upload profile pic?
is this code is valid??
will anyone help me to modify the code?
This Code Is In My Controller File
$employee = new User();
$file = $request->file('file');
$name = time() . $file->getClientOriginalName();
$file->move('uploads/images', $
$employee->file = $file;
$employee->save();
This IS Code Of My view file
<div class="col-md-6">
<div class="form-group">
<label for="photo">Profile Picture :<span class="danger">*</span> </label>
<input type="file" class="form-control" id="file" name="file">
</div>
</div>
Issue is that everything work properly but in my collection of mongodb file isn't store. it upload in folder, but not in database.
You should change $file variable with $name
$employee->file = $name; // $file may contain file object
$employee->save();

Call to a member function getClientOriginalName() on null laravel

I keep on getting this error when I try to upload a pdf document file, does anybody have any idea how to solve this?
I had tried looking for similar problems but still can't solve it(eg: Upload pdf file using Laravel 5)
I tried doing dd() to see if the file have been uploaded and it did show the file name but the error said Call to a member function getClientOriginalName() on null, so now I'm kind of confused on what to do now.
Here are my codes, thanks in advance for helping.
Controller:
class CreateController extends Controller
{
public function create(){
return view('create');
}
public function store(Request $request){
$uniqueFileName = uniqid() . $request->get('upload_file')->getClientOriginalName() . '.' . $request->get('upload_file')->getClientOriginalExtension();
$request->get('upload_file')->move(public_path('files') . $uniqueFileName);
//dd($request);
return redirect()->back()->with('success', 'File uploaded successfully.');
}
create.blade.php
<form enctype="multipart/form-data" class="form-horizontal" method="post" action="{{ url('/user')}}">
{{ csrf_field() }}
<div class="form-group">
<label for="upload_file" class="control-label col-sm-3">Upload File</label>
<div class="col-sm-9">
<input class="form-control" type="file" name="upload_file" id="upload_file">
</div>
</div>
<div class="form-group">
<div class="col-md-6-offset-2">
<input type="submit" class="btn btn-primary" value="Save">
</div>
</div>
</form>
Route:
Route::get('/user/create','CreateController#create');
Route::post('/user','CreateController#store');
This enctype solved function call on the null problem
<form method="" action="" enctype="multipart/form-data">
You need to use the file() function to have access to your uploaded file
$request->file('upload_file')
use this code in your tag form enctype="multipart/form-data"
<form method="" action="" enctype="multipart/form-data">
Try to put the file into a var.
$file = $request->file('upload_file');
And get the extension and name from it.
$uniqueFileName = uniqid() . $file->getClientOriginalName() . '.' . $file->getClientOriginalExtension();
Hope it helps.
The key to solving this problem is that you must write getClientOriginalName() function in the if clause just like that:
if($request->hasFile('logoImage')){
$logoImage = $request->file('logoImage');
$name = $logoImage->getClientOriginalName();
}
if($request->has('sound_file')) {
$image = $request->file('sound_file');
$filename = $image->getClientOriginalName();
request()->$image->move(public_path('images/users'), $filename);
$sounds->image = $request->file('sound_file')->getClientOriginalName();
}

Unable to upload an image in Laravel 5.4

Using Laravel 5.4, I'm tring to setup a form where a user can enter a food name and its image. While trying to upload a PNG or JPG image, I get the following Validation Error:
The image must be an image.
If I remove the validation for image, I get a FatalErrorException:
Call to a member function getClientOriginalExtension() on null
which is a helper function provided by Intervention/Image library. This could mean that the image didn't upload at all.
FORM
<form action="/hq/foods" method="POST">
{{ csrf_field() }}
<input type="text" name="name">
<input type="file" name="image">
<button type="submit" class="btn btn-success ">
ADD FOOD ITEM
</button>
</form>
CONTROLLER
public function store(Request $request) {
$this->validate($request, [
'name' => 'required|min:2|max:255',
'image' => 'required|image'
]);
$food_category = FoodCategory::find($request->food_category_id);
$food = new Food;
$food->name = $request->name;
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('img/foods/' . $filename);
Image::make($image)->resize(800, 400)->save($location);
$food->image = $filename;
$food->save();
Session::flash('success',
'
<h4>Success!</h4>
<p>The food item has been added to the Menu.</p>
');
return back();
}
What am I missing?
To upload images you need to add:
enctype="multipart/form-data"
to your form so instead of:
<form action="/hq/foods" method="POST">
you should use
<form action="/hq/foods" method="POST" enctype="multipart/form-data">
If you use Laravelcollective
{!! Form::open(['method'=>'post','files' => true,'url'=>'/hq/foods']) !!}
OR
Using HTML
<form action="/hq/foods" method="POST" enctype="multipart/form-data">
you need to add enctype="multipart/form-data" in your from

Upload file using Laravel 5.3

I want to upload file in my app.
This is the blade file .
<form action="/fileUploader " files="true" method="post" role="form" name="file" id="chan" >
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="panel panel-default">
<label>Please Select a File to Upload</label>
<input type="image">
<button type="submit" name ="Upload_File">Upload File</button>
</div>
</form>
This is my controller file
public function viewFile()
{
return View::make('/fileUploader');
}
public function showfileupload(Request $request)
{
$file = $request -> file('image');
// show the file name
echo 'File Name : '.$file->getClientOriginalName();
echo '<br>';
// show file extensions
echo 'File Extensions : '.$file->getClientOriginalExtension();
echo '<br>';
// show file path
echo 'File Path : '.$file->getRealPath();
echo '<br>';
// show file size
echo 'File Size : '.$file->getSize();
echo '<br>';
// show file mime type
echo 'File Mime Type : '.$file->getMimeType();
echo '<br>';
// move uploaded File
$destinationPath = 'uploads';
$file->move($destinationPath,$file->getClientOriginalName());
}
This is the web.php file
Route::get('/fileUploader', 'channelController#viewFile');
Route::post('/fileUploader', 'channelController#showfileupload');
I'm getting an error called FatalThrowableError in channelController.php line 48:
Call to a member function getClientOriginalName() on null.
How can I solve this problem
Most likely, you are trying to call a method - getClientOriginalName() - on a object that doesn't exist, so it's null. That jives with the error message you are seeing.
I'm not sure why, but we can start working backwards. Let's use an if statement with the hasFile() method to verify that a file is actually present on the request before attempting to move() it.
if ($request->hasFile('image')) {
$file->move($destinationPath,$file->getClientOriginalName());
}
If you implement the above, does the error still exist?
Here are the Laravel 5.3 Docs on file uploads. It may give you some more ideas.
If you're finding that users are posting files and hasFile() is still returning boolean FALSE, then you may want to go digging into the php.ini file and take a look at the Post_max_size or upload_max_size values to make sure that we aren't blocking large uploads.
VIEW
{!! Form::open(['route'=>'fileUploader', 'id'=>'chan', 'files' => true] )!!}
<div class="panel panel-default">
<label>Please Select a File to Upload</label>
<input type="file" name="image">
<button type="submit">Upload File</button>
</div>
{!! Form::close()!!}
ROUTES
Route::get('/fileUploader', 'channelController#viewFile');
Route::post('fileUploader', array(
'as' => 'fileUploader',
'uses' => 'channelController#showfileupload',
));
CONTROLLER
public function showfileupload(Request $request){
$file = $request -> file('image');
dd($file); // This work well for me and return information about the image
}
Do copy and past! i hope it work, let me know any error and result!

Categories