PHP code example of krma-cl / kcfinder-laravel

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

    

krma-cl / kcfinder-laravel example snippets


use Illuminate\Support\Facades\Gate;

Gate::define('kcfinder.select', static function ($user, string $operation, string $path): bool {
    return $user->can('manage-files');
});

// config/kcfinder.php
'http' => [
    'enabled' => true,
    'prefix' => 'kcfinder',
    'middleware' => ['web', 'auth', 'can:manage-files'],
    // ...
],

use Krma\KCFinder\Laravel\Facades\KCFinder;

$file = KCFinder::select('/01-actos/DO-20130614.pdf');

return response()->json($file);

$previewUrl = KCFinder::previewUrl('/images/photo.jpg');
$selectedUrl = KCFinder::selectedUrl('/images/photo.jpg');

use Krma\KCFinder\Laravel\Contracts\PreviewUrlResolverInterface;
use Krma\KCFinder\Laravel\Contracts\SelectedUrlResolverInterface;

$this->app->bind(PreviewUrlResolverInterface::class, AuthenticatedPreviewResolver::class);
$this->app->bind(SelectedUrlResolverInterface::class, PublicAssetResolver::class);

use Krma\KCFinder\Laravel\Facades\KCFinder;

$result = KCFinder::reportUploaded('/images/photo.jpg');

return response()->json($result, $result->httpStatus());

use Krma\KCFinder\Laravel\Domain\OperationResult;

$result = OperationResult::failure(
    'upload',
    'MIME_NOT_ALLOWED',
    'The detected file type is not allowed.',
    415
);

use Krma\KCFinder\Laravel\Domain\OperationWarning;

$result = KCFinder::reportUploaded('/images/photo.jpg', [
    new OperationWarning(
        'CATALOG_SYNC_FAILED',
        'The file was saved, but catalog synchronization must be retried.'
    ),
]);

use KCFinder\Contract\OperationObserverInterface;

$_LOCALS['_operationObserver'] = app(OperationObserverInterface::class);

use Krma\KCFinder\Laravel\Events\FileMoved;

final class SynchronizeCatalog
{
    public function handle(FileMoved $event): void
    {
        $oldPath = $event->previous->file->path;
        $newPath = $event->file->path;
        $checksum = $event->file->checksum;
        $user = $event->user;
    }
}

$before = KCFinder::snapshot('/images/old-name.jpg', 'rename');

// Perform the authorized rename in the existing KCFinder integration.

$result = KCFinder::reportRenamed($before, '/images/new-name.jpg');
return response()->json($result, $result->httpStatus());

$deleted = KCFinder::snapshot('/images/photo.jpg', 'delete');

// Delete the file.

$result = KCFinder::reportDeleted($deleted);
dotenv
KCFINDER_DISK=public
KCFINDER_URL_PREFIX=/storage
KCFINDER_BROWSER_URL=/kcfinder/browse.php
bash
composer kcfinder:install-assets
php artisan kcfinder:install-assets --force
php artisan kcfinder:clear-cache