Laravel Storage Quick Reference Guide

This quick reference sheet introduces Laravel's Storage facade, explaining its role as a file handling helper and providing concise examples of common operations such as writing, reading, downloading, managing visibility, and manipulating directories and files using the Storage API.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Laravel Storage Quick Reference Guide

Laravel's Storage facade is a convenient file handling helper that abstracts filesystem operations across multiple disks.

The following code snippets demonstrate typical tasks such as writing files, specifying disks, retrieving files, checking existence, downloading, generating URLs, handling temporary URLs, obtaining file size and modification time, managing visibility, appending and prepending content, copying, moving, uploading with custom filenames, and performing directory operations.

// 写入文件
Storage::put('avatars/1', $fileContents);
// 指定磁盘
Storage::disk('local')->put('file.txt', 'Contents');
Storage::get('file.jpg');
Storage::exists('file.jpg');
Storage::download('file.jpg',  $name,  $headers);
Storage::url('file.jpg');
Storage::temporaryUrl('file.jpg', now()->addMinutes(5));
Storage::size('file.jpg');
Storage::lastModified('file.jpg');
// 自动为文件名生成唯一的ID...
Storage::putFile('photos',  new  File('/path/to/photo'));
// 手动指定文件名...
Storage::putFileAs('photos', new File('/path/to/photo'), 'photo.jpg');
Storage::prepend('file.log', 'Prepended Text');
Storage::append('file.log', 'Appended Text');
Storage::copy('old/file.jpg', 'new/file.jpg');
Storage::move('old/file.jpg', 'new/file.jpg');
Storage::putFileAs('avatars', $request->file('avatar'), Auth::id());
// 「可见性」是对多个平台的文件权限的抽象
Storage::getVisibility('file.jpg');
Storage::setVisibility('file.jpg',  'public');
Storage::delete('file.jpg');
Storage::delete(['file.jpg',  'file2.jpg']);
// 获取目录下的所有的文件
Storage::files($directory);
Storage::allFiles($directory);
// 获取目录下的所有目录
Storage::directories($directory);
Storage::allDirectories($directory);
// 创建目录
Storage::makeDirectory($directory);
// 删除目录
Storage::deleteDirectory($directory);
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendPHPstorageLaravelfile management
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.