PHP code example of tihloh / prefab-files

1. Go to this page and download the library: Download tihloh/prefab-files 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/ */

    

tihloh / prefab-files example snippets


use Tihloh\Prefab\Files\FileManager;

$files = new FileManager([
    'root' => __DIR__ . '/storage',
]);

$info = $files->put(
    'documents/report.txt',
    'Hello Prefab',
);

$content = $files->read('documents/report.txt');

$files = new FileManager([
    'default' => 'private',
    'disks' => [
        'private' => [
            'driver' => 'local',
            'root' => __DIR__ . '/storage/private',
        ],
        'public' => [
            'driver' => 'local',
            'root' => __DIR__ . '/public/uploads',
            'url' => '/uploads',
        ],
    ],
]);

$files->put('documents/a.pdf', $contents);

$files->put('avatars/user.jpg', $contents, 'public');

$files->names();
$files->defaultName();
$files->hasDisk('public');
$files->useDefault('public');

$files->put('documents/2026/report.pdf', $contents);

$files->put('exports/report.csv', $csv);

$stream = fopen($sourcePath, 'rb');

try {
    $files->putStream('archives/backup.zip', $stream);
} finally {
    fclose($stream);
}

$files->putFile(
    '/tmp/generated-report.pdf',
    'reports/2026/report.pdf',
);

$result = Input::fromRequest()->process([
    'document' => '

$stored = $files->storeUploaded(
    $result->validated('document'),
    'documents',
);

$files->put('reports/report.pdf', $data);

$files->put('reports/report.pdf', $data, [
    'collision' => 'error',
]);

$stored = $files->put('reports/report.pdf', $data, [
    'collision' => 'rename',
]);

$content = $files->read('documents/report.txt');

$stream = $files->readStream('documents/video.mp4');

try {
    // consume stream
} finally {
    fclose($stream);
}

$info = $files->info('documents/report.pdf');

$info->path();
$info->name();
$info->filename();
$info->directory();
$info->extension();
$info->size();
$info->mime();
$info->modifiedAt();
$info->url();
$info->checksum();
$info->toArray();

$sha256 = $files->checksum('documents/report.pdf');

$sha512 = $files->checksum(
    'documents/report.pdf',
    'sha512',
);

$bytes = $files->size('documents/report.pdf');

$bytes = $files->directorySize('documents');

$bytes = $files->usage();

$url = $files->url(
    'avatars/user.jpg',
    'public',
);

$files = new FileManager([
    'default' => 'private',
    'temporary_url' => '/files/temp',
    'signing_key' => $_ENV['FILES_SIGNING_KEY'],
    'disks' => [
        'private' => [
            'driver' => 'local',
            'root' => __DIR__ . '/storage/private',
        ],
    ],
]);

$url = $files->temporaryUrl(
    'documents/private.pdf',
    600,
);

$valid = $files->verifyTemporaryUrl(
    $_GET['path'] ?? '',
    (int) ($_GET['expires'] ?? 0),
    $_GET['signature'] ?? '',
    $_GET['disk'] ?? null,
);

$download = $files->download(
    'documents/report.pdf',
    'Annual Report 2026.pdf',
);

$download->stream();
$download->filename();
$download->size();
$download->mime();
$download->headers();

$download = $files->download(
    'documents/report.pdf',
    inline: true,
);

$files->copy(
    'documents/a.pdf',
    'archive/a.pdf',
);

$files->move(
    'incoming/a.pdf',
    'documents/a.pdf',
);

$files->delete('documents/a.pdf');

$files->makeDirectory('documents/2026');

$files->directoryExists('documents/2026');

$directories = $files->directories('documents');

$directories = $files->directories(
    'documents',
    recursive: true,
);

$files->deleteDirectory('documents/old');

$files->deleteDirectory(
    'temporary/import-123',
    recursive: true,
);

$list = $files->files('documents');

$list = $files->files(
    'documents',
    recursive: true,
);

$files->supports('checksum');
$files->supports('atomic_write');
$files->supports('local_path');
$files->supports('public_url', 'public');

$files->on('stored', function ($file, $disk, $context) {
    // audit, metrics, indexing, etc.
});

$routes->get('/documents/{id}/download', 'DocumentController@download')
    ->auth()
    ->permission('documents.download');

$download = $files->download($storedPath);

$files->add('cloud', new MyCloudDisk());
$files->useDefault('cloud');

$inputResult = Input::fromRequest()->process([
    'title' => 'trim|($inputResult->fails()) {
    return $inputResult->errors();
}

$data = $inputResult->validated();

$stored = $files->storeUploaded(
    $data['document'],
    'documents/' . date('Y'),
    diskOrOptions: 'private',
);

$record = [
    'title' => $data['title'],
    'stored_path' => $stored->path(),
    'original_name' => $data['document']->name(),
    'mime' => $stored->mime(),
    'size' => $stored->size(),
    'sha256' => $files->checksum($stored->path()),
];

$files->put('report.txt', 'Hello');
text
../.env
../../config.php
documents/../../../secret.txt
text
/uploads/avatars/user.jpg
text
project/
├── public/
│   └── index.php
└── storage/
    └── private/
        └── documents/