'I want to upload image using http post in Laravel but it does not upload - it says failed to open stream: No such file or directory
I make an api in Laravel. After I want to save data to this api using Laravel but I am not able to do this. When I am using Postman data is submitted, but when I use Laravel then they show different error every time.
Here is my Api:
public function store(Request $request)
{
$file = $request->fileName->store('public/files/');
$document = new Product();
$document->code = $request->code;
$document->category = $request->category;
$document->name = $request->name;
$document->price = $request->price;
$document->image = $request->fileName->hashName();
$document->description = $request->description;
$document->save();
return response()->json([
"success" => true,
"message" => "File successfully uploaded",
"file" => $file
]);
}
And this is my class file to upload image:
public function fileUpload()
{
$contents = fopen($this->filename->hashName(), 'r');
$response = Http::attach(
'fileName', $contents)->post('http://127.0.0.1:8000/api/addimg',
[
'code' => $this->code,
'category' => $this->category,
'name' => $this->name,
'price' => $this->price,
'description' => $this->description
]
);
if ($response->json() == null) {
session()->flash('message', 'Failed');
} else {
session()->flash('message', 'File Sucessfully Uploaded');
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
