getting error while uploading file using postman - laravel

I'm trying to upload an image file . When i am calling my API to upload file using postman , getting error like Fatal error: Call to a member function file() on array.
I could not post this file to my Laravel controller .How to post files in Laravel. Anyone could help me to solve this issue?
Here is my controller function,
public function edit(Request $request){
$request = $request->input();
if(empty($request)) {
$request = json_decode(file_get_contents('php://input'),true);
}
$to_return = array();
$file = $request->file('files');
}
at this line $file = $request->file('files'); getting fatal error.

set method as post .. and at body ... change to option from form data to w-xxx-formurlencod ... and try ..

If you want to file uploads with Postman and Laravel, simply remove the Content-Type header setting in Postman.

set method as post
and at body ... select the radio that shows url encoded

see an image ... click on text set it as flie

Related

Adding Image PHPWord in Laravel

So I want to add an header image to my document in PHPWord in Laravel.
So this is my code
public function generateDocx()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$headerLogo = 'http://127.0.0.1:8000/img/logoAnevBulanan.png';
$section->addImage($headerLogo);
// Bunch of line to download the docx
}
And I got Maximum execution time of 60 seconds exceeded, when I try the other method from the documentation, I still got the same error. I try to use asset() helper from laravel and still did not work
Try getting your image using local path instead of URL
$source = file_get_contents('/path/to/my/images/earth.jpg');
$textrun->addImage($source);
Refer to documentation : https://phpword.readthedocs.io/en/latest/elements.html#images

Laravel Api Postman Upload Image Return Null

$files = $request->file('images'); // return {}
$_FILES['images']; // return {"name":"sample-passport.jpg","type":"image\/jpeg","tmp_name":"D:\\xampp\\tmp\\php4AD9.tmp","error":0,"size":264295}
Have you tried to remove the Content-Type header? According to this Github issue, it seems to be a problem.
So, I set up a new Laravel installation to test this out and it's working fine on my side. Of course, there's no authorisation whatsoever but this shouldn't impact the result too much.
routes/api.php
Route::post('/profile/upload_image', function (Request $request) {
dd($request->file('image'));
});
Postman configs
Your post input attribute type change file after upload you will get a response.enter image description here

remove request file from laravel request

When I want upload file with form in laravel, I cant remove file value from request.
This is my full code
if($request->hasFile('image')){
$string_name = str_random(12);
$image = $request->file('image')->move(getcwd().'/image/original',$string_name.'.'.$request->file('image')->getClientOriginalExtension());
$request->request->remove('image');
}
if($request->hasFile('thumbnail')){
$string_name = str_random(12);
$thumbnail = $request->file('thumbnail')->move(getcwd().'/image/thumbnail',$string_name.'.'.$request->file('thumbnail')->getClientOriginalExtension());
}
Portfolio::create($request->all());
but image or thumbnail file do not remove from $request. This means this line of code not working :
$request->request->remove('image');
I've tried many ways but the file does not get removed from the request.
Instead of removing the image from your $request before saving, you can explicitly mention what you would like to save to your model by using the ->only([]) method on $request.
Portfolio::create($request->only(['title', ...]));
This will allow you to specify exactly what you would like saved from the $request data.
You can do the reverse and use the ->except() method to remove the image too:
Portfolio::create($request->except('image'));
use this to remove the file
$request->offsetUnset('input_file_name');
or
$request->except('filename');

Laravel: Function name must be a string

I'm currently working on a project. It was all working just fine until i tried to migrate some tables that I edited. I got this error:
[Symfony\Component\Debug\Exception\FatalThrowableError]
Function name must be a string
Since I doesn't directly show me where the error is, I couldn't find it. Last things I changed before I tried to migrate tables:
Migrations
Laravel colletive/html forms
Store method in my controller
As I know, migrations and forms shouldn't be a problem with this error, so here's my controller code:
public function store(Request $request)
{
$user = Auth::user();
$input = $request->all();
if ($file = $request->file('photo_id')){
$name = time().$file->getClientOriginalName();
$file->move('images', $name);
$photo = Photo::create(['file'=>$name]);
$input['photo_id'] = $photo->id;
}
$user->posts()->create($input);
return redirect('/userPanel');
}
If the error isn't even in a controller code, where could it be. Any help appreciated.
I have found out it was because of a very silly mistake, i was calling a variable as a function...
$ownedMacs = intval($data()['owned_mac']);
Changed to
$ownedMacs = intval($data['owned_mac']);
This error message usually appears when we are doing something real stupid! :)
Same issue in Laravel can solve
Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
Function name must be a string
When you checked on storage/logs/laravel.log
local.ERROR: Function name must be a string {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Function name must be a string at E:\\Project\\workspace\\turttyKidsProject\\app\\Http\\Controllers\\SiteContactController.php:52)
[stacktrace]
Solution
Data field requesting another format but you are trying to save an object.
$objectSave= new ObjectClass;
$objectSave->feild_db = $request->post('request_name');
Check whether you access request with your form submit method. it may be POST or GET

return file response in laravel not working

I'm using laravel 5.2 , I've used response()->file() function to return file. On localhost it is working as expected but on live server file is being downloaded automatically (with no extension). But i wish to open it instead of downlod. Anyone can help?
Here is my code:
public function returnFile($slug)
{$file = Mixes::where('id_name,'=',$slug)->get()->first();
return response()->file('./path/to/file/'.$file->name);}
Thanks.
You'll need to add header to your response.
Response with header example:
$response->header('Content-Type', 'application/pdf');
Simple example:
$file = File::get($file);
$response = Response::make($file, 200);
$response->header('Content-Type', 'application/pdf');
return $response;
Then the file will display in your browser window.
This solution will work with files pdf,docx,doc,xls.
Hope it will help!

Resources