PHP code example of codewiser / simple-files

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

    

codewiser / simple-files example snippets


use Codewiser\Storage\Attachmentable;
use Codewiser\Storage\Storage;
use Codewiser\Storage\StorageContract;
use Illuminate\Database\Eloquent\Model;

class Post extends Model implements Attachmentable
{
    public function storage(): StorageContract
    {
        return new Storage($this, disk: 'public');
    }
}

use Illuminate\Http\Request;

class Controller {
    public function attach(Request $request, Post $post) {

        return $post->storage()->store($request->allFiles()); 
    }
}

use Codewiser\Storage\FileWasStored;

Event::listen(function (FileWasStored $event) {
    $event->path;    // Full path of the file, as the disk resolves it.
    $event->owner;   // The model the file belongs to.
    $event->bucket;  // null, a string or a backed enum case.
});

use Illuminate\Http\Request;

class Controller {
    public function detach(Request $request, Post $post) {
        
        $post->storage()->delete($request->input('unlink'));
    }
}

$post->storage()->flush();

$files = $post->storage()->files();

return $files->toArray();

$post->storage()->toArray();
// Is equivalent to
$post->storage()->files()->toArray();

use Codewiser\Storage\Attachmentable;
use Codewiser\Storage\Storage;
use Codewiser\Storage\StorageContract;
use Codewiser\Storage\Singular;
use Illuminate\Database\Eloquent\Model;

class Post extends Model implements Attachmentable
{
    public function storage(): StorageContract
    {
        return Storage::make($this)->singular();
    }
}

$post->storage()->toArray();
// Is equivalent to
$post->storage()->file()->toArray();

use Codewiser\Storage\Attachmentable;
use Codewiser\Storage\Storage;
use Codewiser\Storage\Singular;
use Codewiser\Storage\StorageContract;
use Illuminate\Database\Eloquent\Model;

class Post extends Model implements Attachmentable
{
    public function storage($bucket = null): StorageContract
    {
        return match ($bucket)
            
            // One cover
            'cover' => Storage::make($this, disk: 'private', bucket: $bucket)
                ->singular(),
                
            // Many docs
            'docs'  => Storage::make($this, disk: 'private', bucket: $bucket),
            
            // Default bucket
            null => Storage::make($this),
            
            default => throw new \InvalidArgumentException("Bucket is not supported"),
        };
    }
}

$docs = $post->storage('docs')->files();
$cover = $post->storage('cover')->file();
$other_files = $post->storage()->files();

use Codewiser\Storage\AttachmentableObserver;
use Illuminate\Database\Eloquent\Attributes\ObservedBy;

#[ObservedBy(AttachmentableObserver::class)]
class Post extends Model implements Attachmentable
{
    // ...
}

class Post extends Model implements Attachmentable
{
    public function storage(Bucket $bucket = null): StorageContract
    {
        return Storage::make($this, bucket: $bucket);
    }
}

use Codewiser\Storage\Attachmentable;
use Codewiser\Storage\Pool;
use Codewiser\Storage\Storage;
use Codewiser\Storage\Singular;
use Codewiser\Storage\StorageContract;
use Illuminate\Database\Eloquent\Model;

class Post extends Model implements Attachmentable
{
    public function storagePool(): Pool
    {
        return Pool::make()
            ->addBucket(Storage::make($this)->singular())
            ->addBucket(Storage::make($this, bucket: 'docs'));        
    }

    public function storage($bucket = null): StorageContract
    {
        return $this->storagePool()->getBucket($bucket);
    }
}

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class PostResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            ...parent::toArray($request),
            
            'files' => $this->storagePool()->toArray()
        ];
    }
}

'local' => [
    'driver' => 'local',
    'root' => storage_path('app/private'),
    'url' => env('APP_URL').'/private',
    'serve' => true,
    'throw' => false,
    'report' => false,
],

use Codewiser\Storage\StorageController;
use Illuminate\Support\Facades\Route;

Route::get('private/{model}/{id}/{bucket}/{filename?}', StorageController::class);