1. Go to this page and download the library: Download timefrontiers/php-file 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/ */
timefrontiers / php-file example snippets
use TimeFrontiers\File\File;
File::configure(
base: [
'default_driver' => 'local', // which driver to use when none is specified
'path_prefix' => 'User-Files', // logical storage namespace — package appends /{owner}
'db_name' => 'file', // database name
'service_url' => 'https://files.example.com', // base URL for your token download endpoint
'token_secret' => 'your-hmac-secret', // used to sign download tokens
'max_size' => 1024 * 1024 * 25, // 25 MB upload limit
'min_size' => 0, // 0 = no minimum
'max_width_px' => 2000, // images wider than this are resized down
'max_height_px' => 2000,
'min_width_px' => null, // null = no minimum dimension check
'min_height_px' => null,
],
drivers: [
'local' => [
'upload_path' => '/var/www/storage', // absolute root on disk
'storage_url' => 'https://cdn.example.com', // base URL for direct public file access
],
's3' => [
'bucket' => 'my-bucket',
'region' => 'us-east-1',
'key' => 'ACCESS_KEY_ID',
'secret' => 'SECRET_ACCESS_KEY',
'endpoint' => null, // set for S3-compatible stores (not MinIO — use 'minio' driver)
'storage_url' => '', // optional CDN override; if empty → native S3 URL
],
'minio' => [
'endpoint' => 'http://localhost:9000', // MinIO server URL (
use TimeFrontiers\File\File;
// Uses base.default_driver — _path is auto-built as /{path_prefix}/{owner}
$file = new File($conn);
// Or select a driver explicitly for this instance
$file = new File($conn, 's3');
$file = new File($conn, 'minio');
$file->privacy('public'); // 'public' | 'private'
$file->owner = $userCode;
$file->caption = 'Profile photo';
// _path is automatically /{path_prefix}/{userCode} — no setPath() needed
$ok = $file->upload($_FILES['avatar'], owner: $userCode, creator: $userCode);
// Override path explicitly when needed (e.g. system files, shared folders):
// $file->setPath('/System/Shared');
if (!$ok) {
// $file->getErrors() — HasErrors compatible
}
echo $file->id; // BIGINT surrogate key
echo $file->code; // '583...' 15-char human identifier
echo $file->sizeAsText(); // '1.2 MB'
// by BIGINT id
$file = (new File($conn))->load(42);
// by 15-char code
$file = (new File($conn))->load('583258614696648');
// via QueryBuilder
$files = File::query()
->where('owner', $userCode)
->where('type_group', 'image')
->orderByDesc('_created')
->limit(20)
->get();
// URL exposes only the unique filename — internal folder structure is never revealed
echo $file->url(); // https://cdn.example.com/abc123def456789.jpg
// Mint a token — expires in 24 hours, max 5 downloads
$token = $file->createToken(
expiresAt: '+24 hours',
maxDownloads: 5,
createdBy: $userCode
);
// Build the full URL to hand to a client
$url = $file->tokenUrl($token);
// → https://files.example.com/download/<token>
// Resolve + validate the token (verifies HMAC, checks expiry and counter)
$file = File::resolveToken($token);
if (!$file) {
http_response_code(403);
exit();
}
$file->download($token); // stream inline (increments counter)
$file->forceDownload($token); // force attachment download