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

Ligonsker's avatar

How can I use File Response to create array an array of images?

I want to display images that users upload to storage/uploads/ folder which is outside of the website's root folder (Because I don't want other users to have access to each other's uploaded photos).

I did a test and tried to use the File Response as described in the docs: https://laravel.com/docs/9.x/responses#file-responses

This is how I did it: In web.php:

Route::get('show_image', [ImageController::class, 'show_image']);

In ImageController:

public function show_image(Request $request)
{
    $pathToFile = '../storage/uploads/460s6DLmCnvoom90S7wfk.jpg';
    return response()->file($pathToFile);
}

Now when I enter the URL of the route in the browser: http://mysite.dev/show_image it displays me the image in the browser inside an img tag with src=http://mysite.dev/show_image (Not a blade file or anything, I simply enter the URL in the browser and it does that automatically.

So far so good, but what I want is to fetch multiple images and have control over them, so not just go to the URL and see the image in full screen.

I need to fetch for example 20 images at once and replace the hard coded array I have of the following form:

const images = [
  {
    id: '1',
    src: 'https://image_api/path_to_some_image1'
  },
    id: '2',
    src: 'https://image_api/path_to_some_image2'
  },
   // more images paths
    id: '20',
    src: 'https://image_api/path_to_some_image20'
  }
];

But, the File Response does not return actual path like the hard coded above, it simply returns the entire image, So how can I actually do it? And not just for one, but an array of images?

I am using Fetch API to call the method above:

const getImagesPromise = fetch('get_images');
getImages
    .then((response) => {
                        
    })
    .then((data) => {

    });

Thanks!

0 likes
4 replies
newbie360's avatar

@ligonsker I just tested to use return response()->file($pathToFile); and it work, but trade-off is show one image = one request

web.php

Route::get('/a', function () {
    return response()->file(storage_path('app/private/a.jpg'));
})->name('a');
<img src="{{ route('a') }}">

so i think base on this information, you can use session, route param or something like that to write a function.

Route::get('/secure-image/{filename}', function ($filename) {
    return response()->file(storage_path('app/private/'. $filename));
});

i think many way/method can do this, think think and think =)
Ligonsker's avatar

@newbie360 thank you! the problem is, I have to fetch about 20 images at a time (infinite scroll), so I'm not sure what is the correct workflow for that - should I send 20 separate requests? Or maybe zip all the files then unzip in the frontend? Or something else?

newbie360's avatar

@Ligonsker well no idea, the simple answer is YES, 20 image = 20 request

and i don't think on run-time unzip is a good idea, but may be preload the action can keep the infinite scroll smooth, well i don't know, unless i do this in my project, may be i can find a solution for this

Ligonsker's avatar

@newbie360 thanks, I might going to try a new approach - to let the web server (nginx in my case) load the images directly. I will try to use nginx X-Accel header for that

Please or to participate in this conversation.