Files with Temporary URLs in Laravel Storage
Generating temporary URLs for files in Laravel's storage allows you to share files securely without exposing them indefinitely. This is especially useful for private files where access needs to be controlled.
Example 1: Generate a Temporary URL for a File
1. Store a File:
First, ensure you have a file stored in your storage. For example, let's assume you've uploaded a file named document.pdf to the public disk.
use Illuminate\Support\Facades\Storage;
Storage::disk('public')->put('documents/document.pdf', 'File content here');2. Generate a Temporary URL: To generate a temporary URL that is valid for a limited time (e.g., 60 minutes), use the temporaryUrl method.
$url = Storage::disk('public')->temporaryUrl('documents/document.pdf', now()->addMinutes(60));
echo $url; // Outputs a temporary URL valid for 60 minutesExample 2: Generating Temporary URLs for Files in Private Storage
If you're using a private disk (e.g., S3), you can still generate temporary URLs.
1. Store a File in Private Disk:
Storage::disk('s3')->put('private/documents/document.pdf', 'File content here');2. Generate Temporary URL:
$url = Storage::disk('s3')->temporaryUrl('private/documents/document.pdf', now()->addMinutes(15));
echo $url; // Outputs a temporary URL for S3You Might Also Like
Route Resource Controllers for CRUD Operations
Resource controllers simplifies CRUD operations and keeps codebase organized and maintainable by fol...
Leverage Chunking for Large Datasets
Process large datasets efficiently by using the chunk method. Chunking retrieves records in smaller...