Upload File in Laravel

Linda Hamilton
发布: 2024-09-19 18:15:03
原创
623 人浏览过

You can upload file in Laravel using its beautiful unified API to interact with many different types of storage systems, from local disk to remote object storage like S3.

As many other Laravel components you can interact with the application filesystem through the Storage Facade: Illuminate/Support/Facades/Storage

This class allows you to access storage drivers called disks that implement a common interface. So you can change the underlying physical storage without touching your application code.

This allows you to not only read and write from your local file system, FTP, or AWS S3 in the same way, but also choose a different option depending on whether you are developing in the local environment or running the application in production.

If you want learn how to build your custom Facade, read the article below:

https://inspector.dev/how-to-extend-laravel-with-driver-based-services/

For more technical articles you can follow me on Linkedin or X.

Configuring storage disks in Laravel

Laravel manages access to the storage via “disks” configured in the config/filesystems.php configuration file. Each disk has an identifying name and a driver that allows you to define the actual type of storage system that will be connected to the application.

Depending on the driver, other options will then be configurable. For example, disks with local drivers require setting the root parameter to indicate which actual local directory they correspond to. Those with s3 drivers require setting the various keys, secrets, regions and buckets to access AWS S3 or S3 compatible systems.

The Public Disk

Among the various disks configured by default, the one named public is one of the most interesting. This disk is designed to host those files that must be made publicly accessible by the Laravel application.

'public' => [
    'driver' => 'local',
    'root' => storage_path('app/public'),
    'url' => env('APP_URL').'/storage',
    'visibility' => 'public',
],
登录后复制

As you can see in the snippet above the disk has local as driver and the root is the private storage/app/public directory.

How can a private directory be publicly accessible?

To allow the public disk to work you have to create a symlink to connect the Laravel public folder to the private root configured in the disk.

Upload File in Laravel

Luckily Laravel has a built-in command to do this setup automatically.

php artisan storage:link
登录后复制

You should run this command the very first time you install your Laravel application on a machine, including your local environment.

When you create a symlink Laravel connects the storage/app/public folder to public/storage folder. That means if you want to display a file in public page you can do:

{{ asset('storage/file.txt') }}
登录后复制

The file is physically stored in storage/app/public, but thanks to the symlink you can access it from the path yourdomain.com/storage/filename.ext

This is needed because the public folder is usually the root of the web server (Apache, NGINX, etc), so it is the only path that your website or app can point to. The symlink makes a portion of the storage folder available to the outside.

Upload file in Laravel

Here is an example of HTML form to allow file uploading:

<form action="/avatar" method="POST" enctype="multipart/form-data"> 
    {{ csrf_field() }}

    <input type="file" name="avatar"/>

    <button type="submit"></button> 
</form>
登录后复制

Saving this file to disk is very easy. The request object has the file method that allows you to get items sent from form field of type file. You can get the item passing the name of the input field into the file method to access the file sent from the frontend.

namespace App/Http/Controllers;

use App/Http/Resources/UserResource;

class AvatarController extends Controller 
{
    public function update(Request $request)
    {
        $path = $request->file('avatar')->store("avatars/{$request->user()->id}");

        $request->user()->update(['avatar' => $path]);

        return new UserResource($request->user());
    }
}
登录后复制

The store method gets the file from the request and saves it on the disk generating a unique ID for the file name, while the file extension is retrieved from the MIME type provided in the request. It returns the path of the saved file, which can therefore be saved or used.

store will use the default disk configured in config/filesystem.php but you can eventually specify a different disk on the fly:

$path = $request->file('avatar')
    ->store("avatars/{$request->user()->id}", 's3');
登录后复制

Upload file using the Storage facade

The same result can be achieved using the Storage facade directly:

$path = Storage::disk('s3')
    ->putFile("avatars/{$request->user()->id}", $request->file('avatar'));
登录后复制

I personally prefer to use the Storage facade because it makes the statement more explicit.

For more technical articles you can follow me on Linkedin or X.

Monitor your Laravel application for free

Inspector is a Code Execution Monitoring tool specifically designed for software developers. You don't need to install anything at the server level, just install the Laravel package and you are ready to go.

If you are looking for HTTP monitoring, database query insights, and the ability to forward alerts and notifications into your preferred messaging environment, try Inspector for free. Register your account.

Or learn more on the website: https://inspector.dev

Upload File in Laravel

以上是Upload File in Laravel的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!