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!

2
  • set url to .env file (default is http://localhost), use asset() for linking public files. Commented Jun 16, 2018 at 16:59
  • I have also tried this solution - no results :( Commented Jun 16, 2018 at 17:01

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

How would I do this with txt, css and js files?
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.

10 Comments

Thanks! I have already tried to use it as well ... but it does not work either :(
what error you are getting ?
can your directly access your file localhost/public/img/boy.png , because 404 error indicate that file does not exists.
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 recheck the name of your file file specially uppercase/lowercase characters. boy.png and Boy.png are not same.
|
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

1 Comment

The image is at this URL: http://localhost:8000/img/boy.png - without public . Just checked it :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.