图像未保存在公用文件夹中

时间:2021-02-22 00:24:23

I have an API, which I have created with Laravel, which accepts images from an Android device, I want to store the image received in public/img folder, but when I check the public\img folder the image isn't there, please what may be the issue, the image location even saves successfully in the db

我有一个API,我用Laravel创建,它接受来自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);
}

2 个解决方案

#1


0  

I got 2 solutions for you

我有2个解决方案给你

1 : In config/filesystems.php, you could do this... change the root element in public

1:在config / filesystems.php中,你可以这样做......在public中更改根元素

'disks' => [
   'public' => [
       'driver' => 'local',
       'root'   => public_path() . '/uploads',
       'url' => env('APP_URL').'/public',
       'visibility' => 'public',
    ]
]

and you can access it by

你可以通过它访问它

Storage::disk('public')->put('filename', $file_content);

2: you can put the file path and try to save it.

2:您可以放置​​文件路径并尝试保存它。

$file = $request->hasFile('photo');
$file->store('toPath', ['disk' => 'public']);

goodluck

#2


0  

You can try replacing

你可以尝试更换

$path = Storage::disk('public')->putFile('img', $request->file('photo')); 

with

$path = Storage::disk('public')->putFile( public_path('img'), $request->file('photo')); 

Had this issue at some point and that was how I resolved it.

在某个时候有这个问题,这就是我如何解决它。

#1


0  

I got 2 solutions for you

我有2个解决方案给你

1 : In config/filesystems.php, you could do this... change the root element in public

1:在config / filesystems.php中,你可以这样做......在public中更改根元素

'disks' => [
   'public' => [
       'driver' => 'local',
       'root'   => public_path() . '/uploads',
       'url' => env('APP_URL').'/public',
       'visibility' => 'public',
    ]
]

and you can access it by

你可以通过它访问它

Storage::disk('public')->put('filename', $file_content);

2: you can put the file path and try to save it.

2:您可以放置​​文件路径并尝试保存它。

$file = $request->hasFile('photo');
$file->store('toPath', ['disk' => 'public']);

goodluck

#2


0  

You can try replacing

你可以尝试更换

$path = Storage::disk('public')->putFile('img', $request->file('photo')); 

with

$path = Storage::disk('public')->putFile( public_path('img'), $request->file('photo')); 

Had this issue at some point and that was how I resolved it.

在某个时候有这个问题,这就是我如何解决它。