Search code examples
phpckeditorckeditor4.xckfinder

How can I automatically save images in different folder with CK Finder 3


I am having difficulty getting this newer version of CKFinder (CKFinder 3) to work like how the old version did. The system used to automatically detect image files and store them in an "images" subfolder and for all other files, in a "files" subfolder.

It now seems the images subfolder only gets files to saved to it when you navigate to the folder in the "browse server" popup window and add files after clicking on the images folder. Uploading and clicking "Send to server" also sends all files to the "files" subfolder regardless of filetype.

The major problem with this is it ignores the file-size rules that are set-up for images, unless you navigate to the images folder, and then it will enforce it. It also is much less convenient as the user will not likely navigate to the subfolder each time.

Currently I have setup the config.php for CKFinder 3 like so:

$config['backends'][] = array(
    'name'         => 'default',
    'adapter'      => 'local',
    'baseUrl'      => $baseDir, // "/mywebsite/sites/default/uploads/"
    //'root'         => '', // Can be used to explicitly set the CKFinder user files directory.
    'chmodFiles'   => 0777,
    'chmodFolders' => 0755,
    'filesystemEncoding' => 'UTF-8',
);


/*================================ Resource Types =====================================*/
// https://ckeditor.com/docs/ckfinder/ckfinder3-php/configuration.html#configuration_options_resourceTypes

$config['defaultResourceTypes'] = '';

$config['resourceTypes'][] = array(
    'name'              => 'Files', // Single quotes not allowed.
    'directory'         => '/files/',
    'maxSize'           => '10M',
    'allowedExtensions' => '7z,aiff,asf,avi,bmp,csv,doc,docx,fla,flv,gz,gzip,mid,mov,mp3,mp4,mpc,mpeg,mpg,ods,odt,pdf,ppt,pptx,pxd,qt,ram,rar,rm,rmi,rmvb,rtf,sdc,sitd,swf,sxc,sxw,tar,tgz,tif,tiff,txt,vsd,wav,wma,wmv,xls,xlsx,zip,gif,jpeg,jpg,png,svg',
    'deniedExtensions'  => '',
    'backend'           => 'default',
);

$config['resourceTypes'][] = array(
    'name'              => 'Images',
    'directory'         => '/images/',
    'maxSize'           => '500K',
    'allowedExtensions' => 'bmp,gif,jpeg,jpg,png,svg',
    'deniedExtensions'  => '',
    'backend'           => 'default',
);

Any help is appreciated, thank you!!


Solution

  • The system used to automatically detect image files and store them in an "images" subfolder and for all other files, in a "files" subfolder.

    This is not correct, CKFinder never worked like this. Although, you can achieve such behavior using a connector plugin.

    Let's say I want all the uploaded images to be placed in Images:/ and other files should go to Files:/.

    <?php
    
    // plugins/MyPlugin/MyPlugin.php
    
    namespace CKSource\CKFinder\Plugin\MyPlugin;
    
    use CKSource\CKFinder\CKFinder;
    use CKSource\CKFinder\Event\BeforeCommandEvent;
    use CKSource\CKFinder\Event\CKFinderEvent;
    use CKSource\CKFinder\Image;
    use CKSource\CKFinder\Plugin\PluginInterface;
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    
    class MyPlugin implements PluginInterface, EventSubscriberInterface
    {
        public function setContainer(CKFinder $app) {}
    
        public function getDefaultConfig() {
            return [];
        }
    
        public static function getSubscribedEvents()
        {
            return [
                CKFinderEvent::BEFORE_COMMAND_FILE_UPLOAD => 'handleBeforeFileUpload',
                CKFinderEvent::BEFORE_COMMAND_QUICK_UPLOAD => 'handleBeforeFileUpload'
            ];
        }
    
        public function handleBeforeFileUpload(BeforeCommandEvent $event) {
            $request = $event->getRequest();
    
            /** @var \Symfony\Component\HttpFoundation\File\UploadedFile $uploadedFile */
            $uploadedFile = $request->files->get('upload');
    
            if ($uploadedFile && $uploadedFile->isValid()) {
                $resourceType = 'Files';
    
                if (Image::isSupportedExtension($uploadedFile->getClientOriginalExtension())) {
                    $resourceType = 'Images';
                }
    
                $request->query->set('type', $resourceType);
                $request->query->set('currentFolder', '/');
            }
        }
    }
    

    Save the above plugin code as plugins/MyPlugin/MyPlugin.php and enable it in config.php:

    $config['plugins'] = ['MyPlugin'];