0

I got a problem while working with Laravel 5.4. I do need to load some pics which are under the public folder public/img. In order to do so, I tried the following lines of code in my blade view:

  1. <img src="{{Config::get('app.url')}}/public/img/boy.png" width="30"/>
  2. <img src="{{url('/img/boy.png')}}" />

In both of the cases, I get this URL which looks (apparently) ok: http://localhost/public/img/boy.png. My application is running fine on this URL: http://localhost:8000/.

The problem is that pic (as well as others in the same folder), cannot be loaded. On the HTML page, I get the following error in the console: http://localhost/public/img/boy.png 404 (Not Found).

How can it be possible? Actually, if I try to go to http://localhost/public/img/boy.png, I get an error that the page does not exist.

How can this problem be solved? How can I get access to my files in the public folder? I have been spending the whole day trying to figure this issue out!

Thank you so much!

Joe
  • 245
  • 2
  • 4
  • 13

3 Answers3

2

add following to your routes and try. I use storage folder instead of public path.

Route::get('/images/{filename}', function ($filename, Illuminate\Http\Request $request)
{
    $path = public_path() . "/img/$filename";
    // $path = storage_path() . "/images/$filename"; //i normally use this, as i put it in storage folder

    //add validations if you want

    if(!File::exists($path)) abort(404);

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

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

    return $response;
});

so now when you go to http://localhost/images/boy.png you will get /public/img/boy.png

aimme
  • 6,385
  • 7
  • 48
  • 65
0

You can use asset It will generate url to png file.

{{ asset('img/boy.png') }}

Update

As suggested in this post you can change public_path()

$app->bind('path.public', function() { return __DIR__; });

Adjust __DIR__ according to your needs.

Amandeep Singh
  • 276
  • 2
  • 13
  • Thanks! I have already tried to use it as well ... but it does not work either :( – Joe Jun 16 '18 at 17:16
  • what error you are getting ? – Amandeep Singh Jun 16 '18 at 17:16
  • can your directly access your file http://localhost/public/img/boy.png , because 404 error indicate that file does not exists. – Amandeep Singh Jun 16 '18 at 17:20
  • No, I cannot directly access my file localhost/public/img/boy.png either - but I have my file under `/public/img/boy.png` . Do not really know how I can get it and what's wrong in my code :( – Joe Jun 16 '18 at 17:55
  • @Joe recheck the name of your file file specially uppercase/lowercase characters. *boy.png* and *Boy.png* are not same. – Amandeep Singh Jun 16 '18 at 18:00
  • I know. I double checked several times - _boy.png_ is the right name – Joe Jun 16 '18 at 18:07
  • I am running the server in this way. Dunno if this can be the problem: `php -S 127.0.0.1:8000 -t public -f serve.php` – Joe Jun 16 '18 at 18:09
  • Check the output of *public_path()* Route::get('testpath', function () { return public_path(); }); – Amandeep Singh Jun 16 '18 at 18:15
  • Thanks! You are right! The _public_path()_ is something like this: `.../myprojectname/public` . I can actually see the pic at this URL `http://127.0.0.1:8000/img/boy.png` ... need to figure out how to omit the "public" part when I want to load an image now – Joe Jun 16 '18 at 18:26
  • Check my updated answer – Amandeep Singh Jun 16 '18 at 18:44
0

Your app is running or http://localhost:8000/ so your image should be this url http://localhost:8000/public/img/boy.png. That makes sense I think. You are running your app on port 8000 so you should specify the port for all your assests

Ulrich Dohou
  • 1,509
  • 14
  • 25
  • 1
    The image is at this URL: `http://localhost:8000/img/boy.png` - without _public_ . Just checked it :) – Joe Jun 16 '18 at 18:55