When working with files and images in Laravel, you can store them for public and private use. In this tutorial, we will explore how to do both, also looking at local server and Amazon S3 setup.
1. Local Server: Storing Public Files
1.1. /public Directory
The public/
directory is the default location where Laravel serves static assets like images. By default, any file in the public directory can be accessed by users using a URL that points to the file.
If we put the cat.jpg
image under the public/images/
folder, it can be accessed via the /images/cat.jpg
URL.
Note: your web server's document root should be set to the
public/
folder of your Laravel project. All files in this directory will be publicly accessible.
It is important to address that if you add any files to the public/
directory during some method calls, and have already configured the repository, your repository will become dirty. So this is suitable only for static files which you do not intend to manage on a software level.
1.2. /storage/app/public Directory
Now let's put the cat.jpg
file in the storage/app/public
directory and try to access it.
The storage/app/public/
directory in Laravel is a directory where you can store files that are publicly accessible to users. This directory provides a convenient location to store user-uploaded files, such as images, videos, and documents.
When files are stored in the storage/app/public/
directory, they are not directly accessible by users via URL. Instead, Laravel provides a symbolic link to the files in the public/
directory, making them accessible to users through a URL.
To create a symbolic link to the storage/app/public
directory, you can use the php artisan storage:link
command. This will create a symbolic link from the public/storage
path to the storage/app/public/
directory.
Once the symbolic link has been created...