PHP code example of themusicdev / files

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

    

themusicdev / files example snippets


'Files' => [
    'backend' => 'local',                       // key into `backends`
    'backends' => [
        'local' => [
            'class' => \League\Flysystem\Local\LocalFilesystemAdapter::class,
            'root' => ROOT . DS . 'data' . DS . 'uploads',   // keep it outside webroot
        ],
    ],
    'collections' => [
        // 'dot.path' => label, MIME allow-list (empty = any), size cap in bytes
        'contracts.pdf' => [
            'label' => 'Contracts',
            'mimes' => ['application/pdf'],
            'size' => 10 * 1024 * 1024,
            // 'public' => true,                // skips the serve gate (see below)
        ],
    ],
    'authCallable' => static function (\TheMusicDev\Files\Model\Entity\StoredFile $file): void {
        // Throw to refuse; return to allow.
    },
],

'Files' => [
    'backend' => 's3',
    'backends' => [
        's3' => [
            'class' => \League\Flysystem\AwsS3V3\AwsS3V3Adapter::class,
            'bucket' => env('FILES_S3_BUCKET'),
            'prefix' => env('FILES_S3_PREFIX', ''),
            'region' => env('FILES_S3_REGION'),
            'key' => env('FILES_S3_KEY'),
            'secret' => env('FILES_S3_SECRET'),
            // Any S3-compatible store (MinIO, R2, Spaces, …):
            // 'endpoint' => 'http://127.0.0.1:9000',
            // 'pathStyle' => true,
        ],
    ],
],

'authCallable' => static function ($file): void {
    $identity = \Cake\Routing\Router::getRequest()?->getSession()->read('Auth.id');
    if ($identity === null) {
        throw new \Cake\Http\Exception\RedirectException(
            \Cake\Routing\Router::url(['plugin' => false, 'controller' => 'Users', 'action' => 'login']),
        );
    }
},

use TheMusicDev\Files\Lib\FileStore;

$store = FileStore::instance();
$row = $store->put($request->getUploadedFile('file'), 'contracts.pdf');   // throws FileStoreException on any intake failure
$id = $row->get('id');                                                    // keep this as <your_table>.file_id

$store->get($id);        // active row, or FileStoreException
$store->read($row);      // contents (bounded by the collection size cap)
$store->delete($row);    // soft delete: row trashed, file kept
$store->purge($row);     // hard delete: row and file