Filesystem
| Disks | |
| Store files | |
| Read back | |
| Public URLs |
Disks
Laravel keeps every file location behind one small API called Storage. Each named disk points at a place where files live: the local disk keeps private files inside the storage folder, while the public disk holds files the web server may serve. The disk list lives in the filesystem config file, so adding a disk never changes calling code.
use Illuminate\Support\Facades\Storage; Storage::disk("local")->put("notes.txt", "Hello");
Store files
The put method writes a string to a path and creates missing folders on the way. Pass a folder prefix to keep uploads sorted, and check exists before overwriting data you want to keep.
use Illuminate\Support\Facades\Storage; Storage::put("reports/q1.txt", "Quarter one"); $exists = Storage::exists("reports/q1.txt"); var_dump($exists);
Read back
The get method returns whole file contents as a string, which fits small text files well. Pair it with exists so missing files show a friendly message instead of an exception, and delete files you no longer need.
use Illuminate\Support\Facades\Storage; if (Storage::exists("reports/q1.txt")) { $text = Storage::get("reports/q1.txt"); echo $text; } Storage::delete("notes.txt");
Public URLs
Files on the public disk get a shareable web address through the url method. Store the file name in the database at upload time, then call url again whenever a page needs the link.
use Illuminate\Support\Facades\Storage; $url = Storage::disk("public")->url("avatars/ada.png"); echo $url;
Next: Localization
Article author: Arthur Isaev