Laravel Dropbox Fire Upload- InvalidArgumentException in Checker.php line 22: - php

I want to upload file with dropbox Api using Laravel. I have used following Code for uploading from my drive to dropbox. But while Uploading I'm getting following Error:
InvalidArgumentException in Checker.php line 22:
'inStream' has bad type; expecting resource, got Illuminate\Http\UploadedFile
If anyone faced or solved the problem, help me to solve it please.
Here is my controller code:
public function dropboxFileUpload()
{
$Client = new Client(env('DROPBOX_TOKEN'), env('DROPBOX_SECRET'));
$dropboxFileName='';
$file = Input::file('image');
$size = Input::file('image')->getSize();
$name = Input::file('image')->getClientOriginalName();
$dropboxFileName = '/'.$name;
$Client->uploadFile($dropboxFileName,WriteMode::add(),$file, $size);
$links['share'] = $Client->createShareableLink($dropboxFileName);
$links['view'] = $Client->createTemporaryDirectLink($dropboxFileName);
print_r($links);
}
Here is the route part:
Route::post('dropboxFileUpload', 'ImportController#dropboxFileUpload');
And here is the view part:
<form style="border: 4px solid #a1a1a1;margin-top: 15px;padding: 10px;" action="{{ URL::to('dropboxFileUpload') }}" class="form-horizontal" method="post" enctype="multipart/form-data" >
<input type="file" name="image" />
{!! Form::token(); !!}
{!! csrf_field() ; !!}
<button class="btn btn-primary">Import File</button>
</form>

Can you please try
$Client->uploadFile($dropboxFileName,WriteMode::add(),$file, $size, null, true);
It's used for indication if the file is for testing purposes. This will skip validation.
Second thing is to check if you create object of \Illuminate\Http\UploadedFile and not \Symfony\Component\HttpFoundation\File\UploadedFile

Related

simple input type file not getting the file

Hi im new to laravel 9 and im trying to upload file from view to controller, but somehow the input file didnt pass the file to controller.
im using laravel 9 with boostrap in it.
heres my code :
view
<form enctype="multipart/form-data" action="{{ route('profile.put') }}" method="POST">
#csrf
#method('POST')
<div class="card-body">
<input id="file" name="file" type="file">
<button type="submit" class="btn btn-block btn-primary">{{ __('Save') }}</button>
</div>
</form>
controller
public function put(Request $request){
return $request;
}
i try to see the $request variable but the result is like this
enter image description here
i try the solution like add enctype="multipart/form-data" but still didnt work.
thank you in advance
I think you simply should get the request by its name or using `$request->all()`, you can do these two things:
In your controller you should write your put method like below:
public function put(Request $request){
$file = '';
if($request->file('file'))
{
$file = $request->get('file);
}
return $file;
}
or you can use the below code in your put method
public function put(Request $request){
return $request->all();
}
Note: If you don't send the file as a JSON Response you should return it to view after saving operation;
and also, there is no need to put #method('POST') inside the form;

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

Uploading images with laravel 5.2

I am trying to create a simple upload images function for my laravel project however I keep getting this error:
FatalThrowableError in UploadController.php line 23:
Fatal error: Call to a member function hasFile() on null
This is my controller:
public function uploadImg(Request $request){
$input = $request->input();
if($input->hasFile('file')){
echo 'Uploading';
$file = $input->file('file');
$file->move('uploads', $file->getClientOriginalName());
echo 'Uploaded';
}
}
This is my form:
<form action="/admin/media/uploadImg" method="post" enctype="multipart/form-data">
<label>Select image to upload:</label>
<input type="file" name="file" id="file">
<input type="submit" value="Upload" name="submit">
<input type="hidden" value="{{ csrf_token() }}" name="_token">
</form>
The hasFile() method only works on the request object, not the input array. Try this instead:
if($request->hasFile('file')){
See: https://laravel.com/docs/5.2/requests#files
You'll also need to change this line:
$file = $input->file('file');
To:
$file = $request->file('file');

Laravel error: MethodNotAllowedHttpException in RouteCollection.php line 219

When i want to insert data in the database i get this error
MethodNotAllowedHttpException in RouteCollection.php line 219
I'm use resource controller
this is my form
<form action="library" method="POST" enctype="multipart/form-data">
{!! csrf_field() !!}
Enter the name of section: <input type="text" name="section_name"> <br>
Upload an image: <input type="file" name="image"> <br>
<button type="submit" class="btn btn-default">Create Section</button>
</form>
and this is my store function
public function store(Request $request)
{
$section_name = $request->input('section_name');
$file = $request->file('image');
$destenationPath = 'iamges';
$filename = $file->getClientOriginalName();
$file->move($destenationPath, $filename);
DB::table('sections')->insert(['section_name' => $section_name, 'image_name' => $filename]);
return redirect('admin');
}
and this my Route
Route::resource('library', 'Main');
You are using action="library", so the form is submitted to library. But, here is nothing to deal with library. You need to submit the form to store() method in Mian controller.
Change action="library" to action="{{ action('Main#store') }}" in form starting tag.
add this route='library.store' to your form:
<form method="POST" route="library.store" enctype="multipart/form-data" files="true">
and your rout should be:
Route::resource('library', 'controller_class_name');
Change you route to:
Route::resource('library', 'MainController');
Also, check out your controller. It should be placed into app\Http\Controllers directory, named MainController.php and it should include this code:
class MainController extends Controller
{
....
public function store(Request $request)
....
}

Categories