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

evilprogrammer's avatar

Image upload using storage function to public folder

I am building a social network using laravel!! So far i love laravel ans thanks to the pple behind it.

Like we all know social networks tend to use alot of images like user gallery and much storage space also.

The thing is i would like to upload images locally then use a queue to send it to S3 in the background. But displaying these images to the broswer becomes a problem since they are stored in storage folder. From the research i have done i could create a route that would return the image file responce but won't that take more time and server resources?? Is there a way i could use the storage function and save the images in the public/uploads folder??

0 likes
10 replies
ohffs's avatar

You can store the image anywhere you like, eg $request->file('photo')->move(public_path("/uploads"), $newfilename);.

5 likes
joedawson's avatar

In config/filesystems.php, you could do this...

'disks' => [

    'local' => [
        'driver' => 'local',
        'root'   => public_path(), // previously storage_path();
    ],

]

Not too sure if this is entirely safe though, but it would work.

2 likes
davidrushton's avatar

We've been doing something similar, storing images in the /storage/app folder for our SaaS app for various reasons. It offers better security, we can do on-the-fly resizing or placeholder/default images with routes, and local/s3/rackspace can be changed easily.

We're keeping an eye on the server resources - as you mentioned using a route to respond with the image loads the Laravel framework, a bit much vs linking directly to the file url. We use the same approach as Intervention when responding with the images, i.e. sending cache headers (https://github.com/Intervention/imagecache/blob/master/src/Intervention/Image/ImageCacheController.php#L136). We also append a unix timestamp (related to the model's updated_at) to get the browser to re-fetch the image when it is changed.

Hope that helps!

ohffs's avatar

I'm in no way affiliated with them - but there's also https://www.imgix.com/ for this kind of thing which I've heard good things about. No idea how the pricing compares to DIY or S3 though. There's also a PHP League library to do similar stuff if you want to DIY : http://glide.thephpleague.com/

pmall's avatar
pmall
Best Answer
Level 56

You can create a new disk

'disks' => [
    'local' => [
        'driver' => 'local',
        'root'   => storage_path(),
    ],
    'uploads' => [
        'driver' => 'local',
        'root'   => public_path() . '/uploads',
    ],
]

Then use it :

Storage::disk('uploads')->put('filename', $file_content);
15 likes
freekmurze's avatar

I've made a package called medialibrary that fits your use case.

Once installed you can do this:

$yourModel->addMedia($yourUploadedFile)-> toMediaLibraryOnDisk('images','s3')

Retrieving the url to a file that's stored in the library is easy as well:

$yourModel->getFirstMediaUrl('images', 'thumb') //returns an url that points to your file on S3

The package uses streams to upload to S3, so upload big files aren't a problem. Of course there's support for using the local filesystem as well. The package also can create thumbnails of your images. The generation of such thumbnails can be queued.

Take a look at the documentation to learn what's possible.

9 likes
chrisreiduk's avatar

Why wouldn't you? Did you look at the package and see what it saves you from implementing?

3 likes
shahmirkj_811's avatar

But what if my location includes some variables like:

'gallery/HouseOwners/' . $email . '/JobID-' . $jobID . '/images/' . $filename

Then how to pass $email and $jobID ?

Please or to participate in this conversation.