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(string|BackedEnum $bucket = null): StorageContract
{
return Storage::make($this, bucket: $bucket);
}
}
use Illuminate\Http\Request;
class Controller {
public function attach(Request $request, Post $post) {
return $post->storage()
->upload($request->allFiles())
->toArray();
}
}
use Illuminate\Http\Request;
class Controller {
public function detach(Request $request, Post $post) {
$post->storage()
->delete($request->input('unlink'));
return response()->noContent();
}
}
$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(string|BackedEnum $bucket = null): StorageContract|Singular
{
return Storage::make($this, disk: 'public', bucket: $bucket)->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;
use BackedEnum;
class Post extends Model implements Attachmentable
{
public function storage(string|BackedEnum $bucket = null): StorageContract|Singular
{
return match ($bucket)
// One cover
'cover' => Storage::make($this, bucket: $bucket)
->singular(),
// Many docs
'docs' => Storage::make($this, bucket: $bucket),
default => throw new \InvalidArgumentException("Bucket $bucket is not supported"),
};
}
}
use Codewiser\Storage\Attachmentable;
use Codewiser\Storage\Storage;
use Codewiser\Storage\Singular;
use Codewiser\Storage\StorageContract;
use Illuminate\Database\Eloquent\Model;
use BackedEnum;
class Post extends Model implements Attachmentable
{
public function storage(string|BackedEnum $bucket = null): StorageContract|Singular
{
return match ($bucket)
// Named bucket
'docs' => Storage::make($this, bucket: $bucket),
// Default bucket
null => Storage::make($this)->singular(),
default => throw new \InvalidArgumentException("Bucket $bucket is not supported"),
};
}
}
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;
use BackedEnum;
class Post extends Model implements Attachmentable
{
public function pool(): Pool
{
return Pool::make()
->addBucket(Storage::make($this)->singular())
->addBucket(Storage::make($this, bucket: 'docs'));
}
public function storage(string|BackedEnum $bucket = null): StorageContract|Singular
{
return $this->pool()->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->pool()->toArray()
];
}
}
use Codewiser\Storage\File;
use Codewiser\Storage\Storage;
use Illuminate\Contracts\Support\Responsable;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
class StorageController
{
public function __invoke(Request $request, string $model, string $id, string $bucket, string $filename = null): Responsable
{
if (is_null($filename)) {
$filename = $bucket;
$bucket = null;
}
$storage = Storage::resolve($model, $id, $bucket);
Gate::authorize('view', $storage->owner());
return $storage->files()->sole(
fn(File $file) => $file->filename() == $filename
);
}
}
use Codewiser\Storage\StorageController;
use Illuminate\Support\Facades\Route;
Route::get('private/{model}/{id}/{bucket}/{filename?}', StorageController::class);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.