Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to access images inside a public folder in Laravel?
If you have an image stored in the public folder of Laravel, you can access or display it in the browser using various methods. In this article, we will learn some of these methods.
Assume we have the image of a flower (flower.jpg) stored in the public/images folder as shown below ?
Now let us use the image to view inside the browser.
Using url() Method
The url() helper function helps to return the full path it will add the HTTP or HTTPS and return the complete path for the given URL ?
<?php
echo url('/images/flower.jpg');
?>
http://127.0.0.1:8000/images/flower.jpg
Example
Now let us see a complete Blade template example to display the image stored in the public folder ?
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: 'Nunito', sans-serif;
}
</style>
</head>
<body class="antialiased">
<div>
<img src="{{url('/images/flower.jpg')}}" alt="Image" width="300" height="250"/>
</div>
</body>
</html>
Using URL::to() Method
The URL::to() method gives the starting path of the URL, for example http://127.0.0.1:8000 or https://127.0.0.1:8000. You can access images by using URL::to('/') inside your blade template ?
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: 'Nunito', sans-serif;
}
</style>
</head>
<body class="antialiased">
<div>
<img src="{{ URL::to('/') }}/images/flower.jpg" alt="Image" width="300" height="250"/>
</div>
</body>
</html>
Using the asset() Helper Function
The asset() helper function also provides the full URL starting from http or https. For example ?
<?php
echo asset('images/flower.jpg');
?>
http://127.0.0.1:8000/images/flower.jpg
Following is the complete example to display the image in the browser using the asset() function ?
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: 'Nunito', sans-serif;
}
</style>
</head>
<body class="antialiased">
<div>
<img src="{{ asset('images/flower.jpg') }}" alt="Image" width="300" height="250"/>
</div>
</body>
</html>
Using public_path() Method
The public_path() method returns the full filesystem path to the public folder. This method is specially used to access files from the public folder programmatically ?
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class UserController extends Controller {
public function index() {
$images = \File::allFiles(public_path('images'));
return View('test')->with(array('images'=>$images));
}
}
?>
test.blade.php
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: 'Nunito', sans-serif;
}
</style>
</head>
<body class="antialiased">
<div>
<ul>
@foreach ($images as $image)
<li style="width:300px;display:inline-block;margin:5px 0px">
<img src="{{asset('images/'. $image->getFilename())}}" width="300" height="250">
</li>
@endforeach
</ul>
</div>
</body>
</html>
Comparison
| Method | Use Case | Output |
|---|---|---|
url() |
Simple URL generation | Full URL with protocol |
URL::to() |
Manual path construction | Base URL + path |
asset() |
Static assets (recommended) | Full URL with protocol |
public_path() |
File system operations | Local filesystem path |
Conclusion
Laravel provides multiple methods to access images in the public folder. Use asset() for displaying images in views, url() for simple URL generation, and public_path() for file system operations.
