24

I upload all user files to directory:

/resources/app/uploads/

I try to get image by full path:

http://localhost/resources/app/uploads/e00bdaa62492a320b78b203e2980169c.jpg

But I get error:

NotFoundHttpException in RouteCollection.php line 161:

How can I get image by this path?

Now I try to uplaod file in directory /public/uploads/ in the root:

$destinationPath = public_path(sprintf("\\uploads\\%s\\", str_random(8)));
$uploaded = Storage::put($destinationPath. $fileName, file_get_contents($file->getRealPath()));

It gives me error:

Impossible to create the root directory 
Dev
  • 1,013
  • 7
  • 15
  • 37

6 Answers6

40

You can make a route specifically for displaying images.

For example:

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;
});

So now you can go to localhost/resources/app/uploads/filename.png and it should display the image.

Alfonz
  • 1,010
  • 13
  • 26
  • 1
    Logic should not be placed in the path, it belongs in the controller or the model. – AndrewRMillar Nov 23 '18 at 06:34
  • I tried this approach using Laravel 8 (ex: http://localhost:8000/files-layout-test/backend-layout-header-tb02-03.jpg) and I get ```Not Found``` message. But when I take away the file name extension (ex: http://localhost:8000/files-layout-test/backend-layout-header-tb02-03), goes through, but I need the file extension to be there. Any ideas on why this could be happening? – Jorge Mauricio May 22 '22 at 17:44
26

You may try this on your blade file. The images folder is located at the public folder

<img src="{{URL::asset('/images/image_name.png')}}" />

For later versions of Laravel (5.7 above):

<img src = "{{ asset('/images/image_name.png') }}" />
Rav
  • 1,327
  • 3
  • 18
  • 32
10

Try {{asset('path/to/your/image.jpg')}} if you want to call it from your blade

or

$url = asset('path/to/your/image.jpg'); if you want it in your controller.

Hope it helps =)

Han Lim
  • 671
  • 7
  • 17
8

As @alfonz mentioned, resource_path() is correct way to get the resource folder directory. To get a specific file location, code will be like the following

 $path = resource_path() . '/folder1/folder2/filename.extension';
Hriju
  • 728
  • 1
  • 16
  • 27
1

First, change your config/filesystems.php

'links' => [
        public_path('storage') => storage_path('app/public'),
        public_path('resources') => resource_path('images'),
    ],

Then normally run

 asset('resources/image.png')

you will get the file URL.

0

Laravel 10 Asset Bundling (Vite)

if you want to process and version all images stored in resources/images you should add the following in your application's entry point resources/js/app.js

import.meta.glob([
  '../images/**',
]);

After then reference these assets in blade like that

<img src="{{ Vite::asset('resources/images/logo.png') }}">

to get it work, need to build npm run build or run Vite development server npm run dev

For more information https://laravel.com/docs/10.x/vite#blade-processing-static-assets

While1
  • 651
  • 4
  • 5