Master Laravel’s File Helper: Essential Methods Cheat Sheet
This concise guide presents Laravel’s File facade methods for common file and directory operations, covering existence checks, reading, writing, moving, copying, permission handling, and cleanup, with clear code examples for each function.
Laravel’s File facade offers a straightforward API for handling files and directories within a PHP application. The following cheat sheet lists the most frequently used methods, each accompanied by a brief description and a ready‑to‑copy code example.
Existence and Basic Retrieval
// Check if a file or directory exists File::exists($path); // Read the contents of a file (optional lock) File::get($path, $lock = false); // Read a file with shared lock File::sharedGet($path); // Retrieve file contents and throw FileNotFoundException if missing File::getRequire($path); // Include a file only once
File::requireOnce($file);Path and Hash Utilities
// Generate an MD5 hash of a file path
File::hash($path);Writing and Modifying Files
// Write contents to a file (optional lock) File::put($path, $contents, $lock = false); // Overwrite an existing file File::replace($path, $content); // Prepend data to the beginning of a file File::prepend($path, $data); // Append data to the end of a file
File::append($path, $data);Permission and Deletion
// Change file or directory permissions File::chmod($path, $mode = null); // Delete one or more files
File::delete($paths);Moving and Copying
// Move a file to a new location File::move($path, $target); // Copy a file to a new location File::copy($path, $target); // Create a hard link
File::link($target, $link);Path Information
// Get filename without extension File::name($path); // Get filename with extension File::basename($path); // Get directory name of a path File::dirname($path); // Get file extension File::extension($path); // Get file type (e.g., file, dir, link) File::type($path); // Get MIME type of a file File::mimeType($path); // Get file size in bytes File::size($path); // Get last modification timestamp
File::lastModified($path);Type Checks
// Determine if a path is a directory File::isDirectory($directory); // Determine if a path is readable File::isReadable($path); // Determine if a path is writable File::isWritable($path); // Determine if a path is a regular file
File::isFile($file);Glob and Directory Listing
// Find paths matching a pattern File::glob($pattern, $flags = 0); // Get all files in a directory (optionally include hidden) File::files($directory, $hidden = false); // Recursively get all files in a directory File::allFiles($directory, $hidden = false); // Get sub‑directories within a directory
File::directories($directory);Directory Management
// Create a directory with optional mode, recursion, and force
File::makeDirectory($path, $mode = 0755, $recursive = false, $force = false);// Move a directory File::moveDirectory($from, $to, $overwrite = false); // Copy a directory to another location
File::copyDirectory($directory, $destination, $options = null);// Delete a directory (optionally preserve files) File::deleteDirectory($directory, $preserve = false); // Recursively delete directories File::deleteDirectories($directory); // Empty a directory of all files and sub‑folders
File::cleanDirectory($directory);Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
