RECUPERAR IMAGEN EN LARAVEL 11
Hace poco expliqué como subir ficheros con laravel 11. Y ahora quiero explicar como recuperar imagen en Laravel 11 cosa que es muy fácil y con el siguiente código se entiende muy bien:public function recuperarImagen($file){
$disk = \Storage::disk("avatars");
if ($disk->exists($file)){
$path = $disk->path($file);
return response()->file($path);
}else{
return response()->json(['status'=>'error', 'mensaje'=>'La imagen no existe']);
}
}
- "$disk = \Storage::disk("avatars");": Se guarda en "$disk" donde se guardan las imágenes.
- "$disk->exists($file)": Se comprueba que el fichero "$file" (nombre del fichero), existe.
- "$path = $disk->path($file);": Si el fichero existe se obtiene la ruta de dicho fichero.
- "return response()->file($path);": Se devuelve el fichero.
- Si el fichero no existe se devuelve un mensaje de error.