lili luna
lili luna

Reputation: 113

Get image from resources folder - Laravel

Can I get and display an image in view from a resources folder instead of public folder? If yes, how can I do that?

Upvotes: 3

Views: 9989

Answers (6)

Giovanni Stroppa
Giovanni Stroppa

Reputation: 31

I would recommend to move the files to the public folder. There are 2 measures I would like to describe to enhance the security here.

  1. Hash the name to avoid/difficult Path Traversal like attacks. So, instead of serial file names like 01.jpg, 02.jpg, something like i3u4b23iu23jib2k3jb4.jpg will be used instead.
  2. If some files must be protected from direct access from the users, store these files on something like public/protected_files/your_file_name.jpg. Then, on Laravel's routes/web.php file, create a route entry where the url path to the protected_files is inside some checkAdmin middleware, allowing only registered as admin users to have access:
Route::middleware('auth')->group(function (){
    // Routes accessible only for authenticated admins
    Route::group(['middleware' => 'admin'], function () {
        Route::get('/protected_files/{filepath}');
    });
});

Upvotes: 0

Sahith Vibudhi
Sahith Vibudhi

Reputation: 5195

I agree with @sehdev.

However, if you still want to serve your image from resources directory, here is a solution that gets the job done.

In your view:

<img src="/your-image" />

In Route:

Route::get('/your-image', function ()
{
   $filepath = '/path/to/your/file';

    $file = File::get($filepath);
    $type = File::mimeType($filepath);

    $response = Response::make($file, 200);
    $response->header("Content-Type", $type);
    $response->header("Content-Length", File::size($filepath));
    return $response;
})

this is not the best solution. I suggest you to move your assets to public directory.

Edit: Use laravel functions. I suggest not to take file path from url because it may subject to Directory Traversal.

Upvotes: 0

albus_severus
albus_severus

Reputation: 3704

You can make a route specifically for displaying images.

Route::get('/resources/app/uploads/{filename}', function($filename){
    $path = resource_path() . '/app/uploads/' . $filename;

    if(!File::exists($path)) {
        return response()->json(['message' => 'Image not found.'], 404);
    }

    $file = File::get($path);
    $type = File::mimeType($path);

    $response = Response::make($file, 200);
    $response->header("Content-Type", $type);

    return $response;
});

now you can go to localhost/resources/app/uploads/filename.png and it should display the image.
reference How to get image from resources in Laravel?
But again say that resources folder should not be used to store images That's not where public, static assets (like images, js, css etc) should be. as @sehdev says his answer..

Upvotes: 1

user5446912
user5446912

Reputation:

You can create an symlink:

ln -s /path/to/laravel/resources/images /path/to/laravel/public/images

Although as other users have already pointed out, the resource directory is not intended to be used publicly.

Upvotes: 0

Sehdev
Sehdev

Reputation: 5662

resources folder should not be used to store images That's not where public, static assets (like images, js, css etc) should be.

Put them inside public/ folder

The resources/assets/ directory is for storing pre-processed assets, so to speak.

For example, if you have 3 different CSS files but want to merge them into one and render that new single file in the browser (to increase page load speed). In this scenario, the 3 CSS files will be put somewhere inside resources/assets/.

These files can then be processed, and the new merged file will go inside public.

Reference:

https://laracasts.com/discuss/channels/laravel/image-assets?page=1

Upvotes: 7

Martin
Martin

Reputation: 121

Anwsear to your question is in Laravel's doc: https://laravel.com/docs/5.7/helpers#method-app-path

$path = base_path('resources/path/to/img_dir');

Upvotes: 0

Related Questions