PHP code example of ez-php / storage
1. Go to this page and download the library: Download ez-php/storage library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ez-php / storage example snippets
return [
'driver' => getenv('STORAGE_DRIVER') ?: 'local',
'local' => [
'root' => getenv('STORAGE_ROOT') ?: 'storage/app',
'url' => getenv('STORAGE_URL') ?: '',
],
's3' => [
'key' => getenv('AWS_ACCESS_KEY_ID') ?: null,
'secret' => getenv('AWS_SECRET_ACCESS_KEY') ?: null,
'region' => getenv('AWS_DEFAULT_REGION') ?: 'us-east-1',
'bucket' => getenv('AWS_BUCKET') ?: null,
'endpoint' => getenv('AWS_ENDPOINT') ?: null, // optional: MinIO, R2, etc.
'url' => getenv('AWS_URL') ?: null, // optional: CDN base URL
'url_expiry' => 3600,
'multipart_part_size' => 8 * 1024 * 1024, // putStream(): larger streams use multipart upload
],
'gcs' => [
'bucket' => getenv('GCS_BUCKET') ?: null,
'access_token' => getenv('GCS_ACCESS_TOKEN') ?: null, // OAuth2 Bearer token; caller refreshes it
'url' => getenv('GCS_URL') ?: null, // optional: CDN base URL
'endpoint' => getenv('GCS_ENDPOINT') ?: null, // optional: API base, e.g. a local emulator
],
];
\EzPhp\Storage\StorageServiceProvider::class,
use EzPhp\Storage\Storage;
Storage::put('avatars/user-1.jpg', $imageBytes);
$data = Storage::get('avatars/user-1.jpg');
Storage::exists('avatars/user-1.jpg'); // true
Storage::url('avatars/user-1.jpg'); // public URL or presigned URL
Storage::delete('avatars/user-1.jpg');
Storage::putUploadedFile('uploads/' . $file->originalName(), $file);
use EzPhp\Storage\StorageInterface;
class AvatarController
{
public function __construct(private readonly StorageInterface $storage) {}
public function upload(Request $request): Response
{
$file = $request->file('avatar');
$this->storage->putUploadedFile('avatars/' . $file->originalName(), $file);
// ...
}
}
use EzPhp\Storage\StorageResponse;
// controller: GET /files/{path}
return StorageResponse::serve($storage, $path, $request, filename: 'report.pdf', contentType: 'application/pdf', inline: true);
// nginx: location /protected/ { internal; alias /var/www/storage/app/; }
return StorageResponse::xAccelRedirect('/protected', $path, 'report.pdf', 'application/pdf');
// Apache mod_xsendfile / lighttpd, LocalDriver only:
return StorageResponse::xSendfile($localDriver, $path, 'report.pdf', 'application/pdf');
$signer = new SignedUrl($_ENV['FILES_SECRET'], 'https://app.test/files'); // secret >= 16 bytes
$link = $signer->make('reports/2026.pdf', ttl: 600); // …?expires=…&signature=…
// in the /files/{path} handler
if (!$signer->verifyRequest($path, $request)) {
return new Response('Forbidden', 403);
}
return StorageResponse::serve($storage, $path, $request, basename($path));
$driver = new LocalDriver('/var/www/storage', 'https://cdn.example.com');
$driver->put('docs/readme.txt', 'Hello');
$driver->url('docs/readme.txt'); // https://cdn.example.com/docs/readme.txt
$driver = new S3Driver('key', 'secret', 'eu-west-1', 'my-bucket');
$driver->put('report.pdf', file_get_contents('/tmp/report.pdf'));
$driver->url('report.pdf'); // presigned URL
$driver = new GcsDriver('my-bucket', $accessToken);
$driver->put('report.pdf', file_get_contents('/tmp/report.pdf'));
$driver->url('report.pdf'); // https://storage.googleapis.com/my-bucket/report.pdf
$signing = new GcsDriver('my-bucket', $accessToken, clientEmail: $sa['client_email'], privateKey: $sa['private_key']);
$signing->signedUrl('report.pdf', 600); // https://storage.googleapis.com/my-bucket/report.pdf?X-Goog-Algorithm=GOOG4-RSA-SHA256&…
use EzPhp\Storage\InMemoryDriver;
$driver = new InMemoryDriver();
$driver->put('invoices/2026.pdf', $bytes);
$driver->exists('invoices/2026.pdf'); // true
$driver->url('invoices/2026.pdf'); // memory://invoices/2026.pdf
$driver->all(); // ['invoices/2026.pdf' => $bytes]
$driver->flush(); // reset between tests
bash
composer