File Facade in Laravel: Unofficial Documentation
- Unveiling Laravel’s File Facade
- Available Methods Under File Facade
- Code Examples Using The File Facade
- Why the File Facade Deserves Your Attention
- Want the Full Breakdown?
Unveiling Laravel’s File Facade
The File facade anchored in the Illuminate\Support\Facades\File namespace is a powerful yet understated part of Laravel’s ecosystem. Unlike the well‑documented Storage facade, its API is less often spotlighted, but it offers a treasure trove of file and directory operations that are perfect for daily dev workflows.
Available Methods Under File Facade
Existence & File State
-
exists(string $path)/missing(string $path)– Confirm presence or absence of a file. -
isReadable(string $path),isWritable(string $path),isFile(string $file),isDirectory(string $directory),isEmptyDirectory(string $directory, bool $ignoreDotFiles = false)– State checks to avoid surprises.
File Reading & Data Extraction
-
get(string $path, bool $lock = false)– Read file content with optional locking. -
json(string $path, int $flags, bool $lock = false)– Read & decode JSON straight off disk. -
lines(string $path)→ returns aLazyCollection– Iterate files line‑by‑line without slurping into memory.
Integrity, Hashing & Comparisons
-
hash(string $path, string $algorithm = 'md5')– Compute file hash (MD5 default). -
hasSameHash(string $firstFile, string $secondFile)– Instantly compare file content equality via hash.
Writing & Text Modifications
-
put(string $path, string $contents, bool $lock = false)– Write to file with optional lock. -
replace(string $path, string $content, int|null $mode = null)– Overwrite entire file. -
replaceInFile(array|string $search, array|string $replace, string $path)– Inline search-and-replace. -
prepend(string $path, string $data),append(string $path, string $data, bool $lock = false)– Add content at the start or end of a file. -
chmod(string $path, int|null $mode = null)– Change file permissions.
File & Directory Manipulation
-
delete(string|array $paths),move(string $path, string $target),copy(string $path, string $target)– CRUD essentials for files. -
link(string $target, string $link),relativeLink(string $target, string $link)– Create absolute or relative symlinks. -
Directory operations:
-
ensureDirectoryExists(string $path, int $mode = 0755, bool $recursive = true) -
makeDirectory(string $path, int $mode = 0755, bool $recursive = false, bool $force = false) -
moveDirectory(string $from, string $to, bool $overwrite = false) -
copyDirectory(string $directory, string $destination, int|null $options = null) -
deleteDirectory(string $directory, bool $preserve = false) -
deleteDirectories(string $directory) -
cleanDirectory(string $directory)– All the tools to safely create, copy, move, clean up, or delete directories.
-
Pattern Matching & Listings
-
glob(string $pattern, int $flags)– Pattern-based search. -
files(string $directory, bool $hidden = false)/allFiles(string $directory, bool $hidden = false)– List files, optionally including hidden ones. -
directories(string $directory)– List subdirectories.
Conditional Logic & Extensibility
-
when(...),unless(...)– Conditional workflows on file operations. -
Laravel macro system support:
-
macro(...),mixin(...),hasMacro(...),flushMacros()– Add or manage custom behavior at runtime.
-
Facade & Testing Tools
Methods inherited from the base Facade class:
-
resolved(),spy(),partialMock(),shouldReceive(),expects(),swap(),isFake(),getFacadeRoot(),getFacadeAccessor(),resolveFacadeInstance()– useful in testing and mocking.
Code Examples Using The File Facade
// Stream file safelyFile::lines($path)->each(fn($line) => process($line)); // Lock‑safe writeFile::put($path, $contents, true); // Parse JSON$data = File::json($configFile, JSON_THROW_ON_ERROR, true); // Inline replacementsFile::replaceInFile('TODO', 'DONE', $filePath); // Directory managementFile::ensureDirectoryExists($dirPath);File::cleanDirectory($dirPath); // SymlinksFile::link($targetFile, $symlink); // Custom macroFile::macro('duplicateAndDelete', function ($path) { File::copy($path, $path.'.dup'); File::delete($path);});
Why the File Facade Deserves Your Attention
| Use Case | Benefit |
|---|---|
| Resource-efficient streaming | lines() avoids memory spikes on big files |
| Safer concurrent operations | Lockable reads/writes reduce race condition risks |
| Convenient file content manipulation | Inline search/replace, prepend/append simplify patterns |
| Robust directory handling | One-liners like ensureDirectoryExists() keep code clean |
| Custom workflows | macro() and when() let you tailor file logic elegantly |
| Tightly testable | Fully integrates with Laravel’s mocking tools via facade |
Want the Full Breakdown?
Head over to the official Laravel API docs for the File facade to explore everything in detail:
Stay Updated.
I'll you email you as soon as new, fresh content is published.