【问题标题】:How can I automatically save images in different folder with CK Finder 3如何使用 CK Finder 3 自动将图像保存在不同的文件夹中
【发布时间】:2019-03-19 18:03:12
【问题描述】:

我很难让这个新版本的 CKFinder (CKFinder 3) 像旧版本一样工作。该系统用于自动检测图像文件并将其存储在“images”子文件夹中,对于所有其他文件,则存储在“files”子文件夹中。

现在,当您导航到“浏览服务器”弹出窗口中的文件夹并在单击图像文件夹后添加文件时,似乎图像子文件夹只会将文件保存到其中。无论文件类型如何,上传并单击“发送到服务器”也会将所有文件发送到“文件”子文件夹。

这样做的主要问题是它忽略了为图像设置的文件大小规则,除非您导航到图像文件夹,然后它将强制执行。它也不太方便,因为用户不太可能每次都导航到子文件夹。

目前我已经为 CKFinder 3 设置了 config.php,如下所示:

$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',
);

感谢您的帮助,谢谢!

【问题讨论】:

    标签: php ckeditor ckeditor4.x ckfinder


    【解决方案1】:

    系统用于自动检测图像文件并将它们存储在“images”子文件夹中,对于所有其他文件,则存储在“files”子文件夹中。

    这是不正确的,CKFinder 从来没有这样工作过。不过,您可以使用连接器插件来实现这种行为。

    假设我希望所有上传的图片都放在Images:/,其他文件应该放在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', '/');
            }
        }
    }
    

    将以上插件代码保存为plugins/MyPlugin/MyPlugin.php,并在config.php中启用:

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

    【讨论】:

    • 这似乎可以解决问题,非常感谢。我一定是在我的旧版本 CKFinder 中使用了一个插件,它在幕后为我做这件事。我注意到这个解决方案的一个小怪癖是,在大 CK Finder 窗口出现之前的“上传”选项卡有一个“发送到服务器”选项。这个“发送到服务器”按钮仍然会将所有内容放入该文件目录中。你知道我怎样才能让它以同样的方式工作吗?
    • @Wallaaa:从 CKEditor 上传使用名为 QuickUpload 的单独命令。我修改了答案并添加了以下行:CKFinderEvent::BEFORE_COMMAND_QUICK_UPLOAD =&gt; 'handleBeforeFileUpload'。这应该可以解决问题。
    • 这很好用,新的插件代码解决了这个问题。再次感谢 Zaak!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-18
    • 2013-06-23
    • 1970-01-01
    • 1970-01-01
    • 2022-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多