简体   繁体   中英

How to check from full image url that the image exists in storage folder or not in laravel

I have stored full url of image when store the image in database.

$image = $request->hasFile('thumbnail') ? Storage::disk('public')->put('templates', $request->file('thumbnail')) : null;
'thumbnail' => Storage::disk('public')->url($image)

Now I need to check if the image exists in storage templates folder or not. when retrieve http://my-domain//storage/templates/vglZJtHqvNrp5gD6p7A9KL79CIGG2KIn2Pe1e8po.png that i got in image. How can i check this?

2 Approach you can use but i need to know the exact directory path, public/templates/.. or storage/templates/..

Method 1

file_exist(storage_path('/templates/vglZJtHqvNrp5gD6p7A9KL79CIGG2KIn2Pe1e8po.png')); //Storage_path or public_path

Method 2

$exists = Storage::disk('public/templates/')->exists('vglZJtHqvNrp5gD6p7A9KL79CIGG2KIn2Pe1e8po.png'); //   public/templates/ or storage/templates

I would recommend the first method.

You could simply replace the base storage URL to get the path:

// use `url` to guarantee an absolute URL
$baseStorageUrl = url(Storage::url('/'));
$path = str_replace($baseStorageUrl, '', $imageURL);
$exists = File::exists('public' . $path);

(untested, but should work)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM