我有一个用Laravel创建的API,该API接受来自Android设备的图像,我想将收到的图像存储在public/img
文件夹中,但是当我检查public\img
文件夹时,图片不存在,请问可能是问题所在,图片位置甚至成功保存在数据库中
public function SellPost(Request $request){
$user = User::where('id', $request->user_id)->first();
$goods = new Goods;
$goods->name = $request->name;
if($request->description){
$goods->description = $request->description;
}
$goods->category_id = $request->cat_id;
$goods->amount = $request->amount;
$goods->quantity = $request->qty;
$goods->seller_id = $request->user_id;
if($goods->save()){
if($request->hasFile('photo')){
$path = Storage::disk('public')->putFile('img', $request->file('photo'));
$filename = basename($path);
// Image::make($images->uri)->save(public_path('img/' . $filename));
$saver = new Images;
$saver->product_id = $goods->id;
$saver->location_url = 'img/'.$filename;
$saver->save();
// $this->imagesUpload($goods->id, $request->photo);
}else{
return response()->json(['error'=>'Internal Server Error, Please try again Later'], 401);
}
};
return response()->json(['success'=>'Goods Posted Successfully'], $this->successStatus);
}
答案 0 :(得分:0)
您可以尝试替换
$path = Storage::disk('public')->putFile('img', $request->file('photo'));
与
$path = Storage::disk('public')->putFile( public_path('img'), $request->file('photo'));
在某个时候出现了这个问题,这就是我解决的方法。
答案 1 :(得分:0)
我为您提供2种解决方案
1:在config / filesystems.php中,您可以执行此操作...在公共目录中更改根元素
'disks' => [
'public' => [
'driver' => 'local',
'root' => public_path() . '/uploads',
'url' => env('APP_URL').'/public',
'visibility' => 'public',
]
]
您可以通过以下方式访问它
Storage::disk('public')->put('filename', $file_content);
2:您可以放置文件路径并尝试保存它。
$file = $request->hasFile('photo');
$file->store('toPath', ['disk' => 'public']);
好运