ForgeStorage Module
Overview
ForgeStorage is a flexible file storage module that provides an S3-compatible interface for managing files and buckets. Currently, it includes a LocalDriver implementation with planned support for S3 and other storage providers.
Configuration
Configure the storage system through the forge_storage.php
configuration file:
return [
'default_driver' => 'local',
'root_path' => 'storage/app',
'public_path' => 'public/storage',
'buckets' => [
'uploads' => [
'driver' => 'local',
'public' => false,
'expire' => 3600 // 1 hour
]
]
];
Configuration Options
default_driver
: The default storage driver to use (currently 'local')root_path
: Base storage path for private filespublic_path
: Public storage path for accessible filesbuckets
: Configuration for storage buckets
Commands
ForgeStorage provides two main commands for managing storage links:
storage:link
Creates a symbolic link from "public/storage" to "storage/app/public":
php forge.php storage:link
storage:unlink
Removes the symbolic link from "public/storage":
php forge.php storage:unlink
Usage
Here's how to use the storage system in your application:
Basic File Operations
use Forge\Core\Helpers\App;
$storage = App::storage();
// Store a file
$storage->put('uploads', 'path/to/file.jpg', $fileContents);
// Get a file URL
$url = $storage->getUrl('uploads', 'path/to/file.jpg');
// Get a temporary URL
$tempUrl = $storage->temporaryUrl('uploads', 'path/to/file.jpg', time() + 3600);
Bucket Management
// Create a new bucket
$storage->createBucket('new-bucket');
// Check if a bucket exists
$exists = $storage->bucketExists('new-bucket');
Storage Dashboard
ForgeStorage includes a built-in dashboard for managing files and buckets:
Features
- Create and manage buckets
- Upload files to buckets
- View file details (size, path, etc.)
- Generate public and temporary URLs
Dashboard Implementation
The dashboard is implemented through:
storage/dashboard.php
- Dashboard view templateDashboardController.php
- Controller handling storage operations
Secure File Access
ForgeStorage provides secure file access through hashed URLs and temporary access links:
// Route configuration for secure file access
$router->get('/files/{clean_path}', [FileController::class, 'serveFile'],
[FileExpirationMiddleware::class]);
URL Security Features
- File paths are hashed for security
- Temporary URLs with configurable expiration
- FileExpirationMiddleware enforces URL expiration
- Clean URL routing through .htaccess configuration
.htaccess Configuration
# Rewrite rule for secure file access
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^files/(.+)$ index.php?file=$1 [L,QSA]
The .htaccess configuration ensures clean URLs for file access while maintaining security through PHP routing.