Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lihaibh's avatar

How to get public url of an uploaded file in Laravel 5

Hello everyone, Ive started to use flysystem package and i uploaded some images. My problem is when im trying to display those images in my html page, i need a url.

First of all, is there any "clean" way to do it? as flysystem is generic library i hope that i could do something like this:

Storage::disk('my_awesome_disk')->publicUrl($path)

for 'public' files so if im using s3 bucket as my driver for the disk i will get: "https://s3.amazonaws.com/my_awesome_bucket/path/image.png"

or alternatively:

Storage::disk('my_awesome_disk')->signedUrl($path, $timeout)

for 'private' files.

Is this something i can achieve only with specific implementation? for example if im using Amazon S3 i can easily run:

$signed_url = $s3Client->getObjectUrl($bucket_name,
                            $resource_key,
                            "+{$expires} minutes");

But i dont want to do ugly switch case to determine what driver im using. And how can i combine cdn (like cloudfront)? Any suggestion?

0 likes
9 replies
michaeldyrynda's avatar

Flysystem is about dealing with files on some storage medium, be it S3, local, or some other supported driver.

There is no way to get a public URL as such using fly system, you have to use the underlying driver and the call will vary from driver to driver.

Your best bet would be to extend flysystem in your app - or create a package - that will handle retrieving a public URL.

You could also do some handling with Response::download (I think) and create a route to retrieve files that way from storage. The benefit here is you can do things like check users have permission to access the resource. This is ok with local files, but beware that if you're using S3 for example, it'll pull the file down to your server then stream it back again - which isn't ideal.

lihaibh's avatar

Thats really messing up my head. Right now i implement chain of responsibility pattern to handle public urls by priority (cdn than s3), but again need to check if im using s3 if not than i return null T_T cuz i dont know how to handle public urls for other sources..

martinbean's avatar

@lihaibh Flysystem works with paths, not URLs. Your application needs to map a file path to a URL.

By default, I think Laravel uses storage/app as the storage root, which isn’t web-accessible. You’ll need to either change the storage root to a web-accessible location, or move the file (and optionally create any thumbnails) to a web-accessible directory.

scofield's avatar

Don't forget to set public parameter when uploading the file

Storage::disk('s3')->put($fileName, $fileContents, 'public');

then

Storage::disk('s3')->url($fileName)
6 likes
bansal's avatar

Storage::disk('s3')->url($fileName) is working in laravel 5.2 and above.

But iin laravel 5.1 disk url method is not working. Is there any solution for that.

1 like
ankush981's avatar

@MGUYZ - Thanks! This was EXACTLY what I was looking for. I had configured a 'public' disk as shown in the docs (by creating a symbolic link and all) but was going nuts trying to generate a URL for the front-end. :-)

Please or to participate in this conversation.