【问题标题】:How to change the default browse directory for images in KCFinder?如何更改 KCFinder 中图像的默认浏览目录?
【发布时间】:2017-11-02 20:19:58
【问题描述】:

我将 CKEditor 与 KCFinder 一起使用。除了一件事,一切都很好。

我的默认上传网址是:

'uploadURL' => "/userfiles"

假设我想上传一张图片。我在 CKEditor 中单击上传图像,然后单击浏览服务器以选择已上传的图像。问题是 CKEditor 正在浏览 /userfiles/images 而不是 /userfiles 像我想的那样。

我知道这不是错误,只是一种设置方式,但我该如何更改它?

我在http://kcfinder.sunhater.com 上没有找到任何东西

【问题讨论】:

标签: php javascript ckeditor


【解决方案1】:

我找到了更好的解决方法。

当您在 config.php 文件中定义:

'types' => array(
        // CKEditor & FCKEditor types
        'files'   =>  "disabled",
        'flash'   =>  "disabled",
        'images'  =>  "*img",

        // TinyMCE types
        'file'    =>  "disabled",
        'media'   =>  "disabled",
        'image'   =>  "*img",
    ),

这表示 KCfinder 将创建一个“图像”目录并存储图像。我禁用了“文件”目录,因为我不允许我的用户上传文件。

如果您希望它们位于根目录,您要做的就是添加一个 '' 条目(空白),并指定它们可以上传的内容。在我的服务器中,我有:

'types' => array(
        '' => "*img",
        // CKEditor & FCKEditor types
        'files'   =>  "disabled",
        'flash'   =>  "disabled"
    ),

这让我可以将图像文件直接上传到根文件夹。

【讨论】:

  • 实际上,您可以只保留一个类型,它的工作方式相同(或者至少在与 Drupal 集成时可以工作)。所以类型配置可以简单地是:'types' => array('' => "*img"),.
【解决方案2】:

我遇到了同样的问题,直到我尝试了以下会话参数(动态设置;http://kcfinder.sunhater.com/docs/install#dynamic):

$_SESSION['KCFINDER']['uploadURL'] = '/some_directory/some_subdir'; $_SESSION['KCFINDER']['uploadDir'] = '../some_directory/some_subdir';

“uploadURL”必须引用“html”路径,即您的 html 标签可以识别的服务器根目录的绝对路径。例如,如果您的文件位于 Web 服务器根目录下名为“images”的目录下,您将拥有:

$_SESSION['KCFINDER']['uploadURL'] = '/images';

“uploadDir”必须是指包含您希望访问的文件的目录的相对路径或从您的 DISK 根目录开始的绝对路径(例如:/var/www/html/.....)。我认为最好使用 KCFinder 安装路径位置的相对路径,因为这样可以移植。 继续上面的例子,考虑到 KCFinder 安装在 /web_apps/kcfinder 中,那么你会得到这个:

$_SESSION['KCFINDER']['uploadDir'] = '../../images';

希望这会有所帮助!

【讨论】:

  • 感谢苏迪尔的回复!我已经尝试了您推荐的内容,但它仍然添加了文件夹图像。 $_SESSION['KCFINDER']['uploadURL'] = '/userfiles'; $_SESSION['KCFINDER']['uploadDir'] = '../../../userfiles'; KCFinder 在 site.com/admin/ckeditor/kcfinder/ 如果我输入 $_SESSION['KCFINDER']['uploadDir'] = '/userfiles';它给了我一个错误“无法写入上传文件夹。/userfiles”
【解决方案3】:

CKEDITOR/FCKEDITOR 中文件类型的自有文件夹: 如果你想使用自己的文件夹来保存图片,文件只需在 CKEDITOR/FCKEDITOR config.js 中配置

这行:

    //KCFINDER
CKEDITOR.editorConfig = function( config ) {
   config.filebrowserBrowseUrl = '/kcfinder-2.51/browse.php?type=file';
   config.filebrowserImageBrowseUrl = '/kcfinder-2.51/browse.php?type=image';
   config.filebrowserFlashBrowseUrl = '/kcfinder-2.51/browse.php?type=flash';
   config.filebrowserUploadUrl = '/kcfinder-2.51/upload.php?type=file';
   config.filebrowserImageUploadUrl = '/kcfinder-2.51/upload.php?type=image';
   config.filebrowserFlashUploadUrl = '/kcfinder-2.51/upload.php?type=flash';
};

例如,如果您想为您使用图像文件夹“ImagesMyOwnFolder”==> 将行更改为 config.filebrowserImageBrowseUrl = /kcfinder-2.51/browse.php?type=ImagesMyOwnFolder';

希望这可以帮助其他想要使用自己的文件夹或来自其他文件管理器的现有文件夹,这些文件夹已经存在并且无法移动。

【讨论】:

  • 这绝对不适合我。如果您将 ImageBrowseUrl 的 'type=image' 更改为其他任何内容,例如 ImagesMyOwnFolder,它会忽略它,而是默认为一个名为 'file' 的目录。
  • 你的意思不是type= 我认为是dir= 这也没有解决问题
【解决方案4】:

它可以通过多种方式完成。我正在解释一个过程,我根据我的 php 应用程序的代码结构应用了该过程。我为不同的应用程序遵循相同的代码结构/框架,每个应用程序都作为我服务器中的子文件夹。因此,合乎逻辑的需要将一个单一的 CKeditor 与 KCfinder 一起使用并以某种方式对其进行配置,以使其适用于所有应用程序。 CKeditor的内容部分还可以。它可以很容易地被来自单个 CKeditor 组件的不同应用程序或项目重用。但是文件上传会出现问题,例如图像、视频或任何其他文档。为了使其适用于不同的项目,文件必须上传到不同项目的单独文件夹中。而对于 $_CONFIG['uploadURL'] 必须通过配置动态文件夹路径,意味着每个项目的不同文件夹路径,但在同一位置调用相同的 CKeditor KCfinder 组件。我正在逐步解释一些不同的过程。这些对我来说在 KCfinder 2.51 版上工作得很好,我希望他们也能在其他人身上工作。如果它不适用于其他开发人员,那么他们可能需要根据他们的项目代码结构和文件夹写入权限以及 CKeditor 和 KCfinder 版本在这些过程中进行一些调整。

1) 在CKeditor\filemanagers\kcfinder_2_51\config.php 文件中

a) 在 $_CONFIG 数组定义中,搜索此行 'disabled' => false,如果发现任何替换为 'disabled' => true, 在该文件的末尾放置以下代码。该代码是自我解释的,其中注释掉了逻辑和细节。代码是:

//Code to assign $_CONFIG['uploadURL'] dynamic value: different for different projects or sites: added by Mrinal Nandi on 5 oct, 2013: start
//session dependent dynamic $_CONFIG['uploadURL'] setting :start 

////session dependent secure method: only for single site setting: i.e. one CKeditor KCfinder for each one project domain or subdomain, not one CKeditor KCfinder for multiple project:start    
//  session_start();
//if(isset($_SESSION['KCFINDER']['uploadURL']) && $_SESSION['KCFINDER']['uploadURL']!="") { //$_SESSION['SESSION_SERVER_RELATIVEPATH']: relative folder path of the project corresponding to the webroot; should be like "/project/folder/path/"  //set this session variable in a common file in your project where the session started 
//  $file_upload_relative_path=$_SESSION['KCFINDER']['uploadURL'];  
//}
////session dependent secure method: only for single site setting: i.e. one CKeditor KCfinder for each one project domain or subdomain, not one CKeditor KCfinder for multiple project:start


//Using a single CKeditor KCfinder component for different projects or sites (multisite): start

//session dependent settings a single CKeditor KCfinder component for different projects or sites (multisite): start 
//Assuming different session_name for different projects, if represented as different sub-folders, but not work if represented as sub-domains or different domains 
//Secure and deny access for unauthorized users without any session, thus restrict access via direct link  
//but not work if projects represented as sub-domains or different domains, then have to use the session independent way provided bellow (though it is insecure), or have to implement some session related way as per the project flow and structure   

session_name(base64_decode($_REQUEST['param_project'])); 
session_start();    

if(isset($_SESSION['KCFINDER']['uploadURL']) && $_SESSION['KCFINDER']['uploadURL']!="") { //$_SESSION['SESSION_SERVER_RELATIVEPATH']: relative folder path of the project corresponding to the webroot; should be like "/project/folder/path/"  //set this session variable in a common file in your project where the session started 
    $file_upload_relative_path=$_SESSION['KCFINDER']['uploadURL'];  

}
//session dependent settings a single CKeditor KCfinder component for different projects or sites (multisite): end 



//session dependent dynamic $_CONFIG['uploadURL'] setting :end 

////session independent dynamic $_CONFIG['uploadURL'] setting: without using session :start
//if(isset($_REQUEST['param_project']) && $_REQUEST['param_project']!=""){ //base64 encoded relative folder path for file upload in the project,  corresponding to the webroot; should be like "/project/folder/file/upload/path/" before encoding 
//  $file_upload_relative_path=base64_decode($_REQUEST['param_project']);
//  
//}
////session independent dynamic $_CONFIG['uploadURL'] setting: without using session :end 


if(isset($file_upload_relative_path) && trim($file_upload_relative_path)!="" ){
    if(isset($_SESSION['KCFINDER']['uploadURL'])){
        $_CONFIG['disabled']=false;
    } else if(is_dir($file_upload_relative_path)) { //to make it relatively secure so that hackers can not create any upload folder automatcally in the server, using a direct link and can not upload files there 
        $_CONFIG['disabled']=false;
    }
}
// Path to user files relative to the document root.
$_CONFIG['uploadURL']= $file_upload_relative_path;
$_CONFIG['param_project'] = $_REQUEST['param_project'];
//Using a single CKeditor KCfinder component for different projects or sites (multisite): end 

//Code to assign $_CONFIG['uploadURL'] dynamic value: different for different projects or sites: added by Mrinal Nandi on 5 oct, 2013: end 

2) 在 ckeditor\filemanagers\kcfinder_2_51\js\browser\misc.js 中

搜索这一行:var data = 'browse.php?type=' + encodeURIComponent(this.type) + '&lng=' + this.lang;

用该行替换它:

var data = 'browse.php?type=' + encodeURIComponent(this.type) + '&lng=' + this.lang + '&param_project=' + this.param_project;

3) 在 ckeditor\filemanagers\kcfinder_2_51\tpl\tpl_javascript.php 中

搜索这一行:browser.type = "type) ?>";

将这些命令放在该行之后:

browser.param_project = "<?php echo text::jsValue($this->config['param_project']) ?>";

4) 在 ckeditor\filemanagers\kcfinder_2_51\core\uploader.php 在 __construct() 函数中搜索此行:

if (isset($this->config['_check4htaccess']) &&
    $this->config['_check4htaccess']
) {
    $htaccess = "{$this->config['uploadDir']}/.htaccess";
    if (!file_exists($htaccess)) {
        if (!@file_put_contents($htaccess, $this->get_htaccess()))
            $this->backMsg("Cannot write to upload folder. {$this->config['uploadDir']}");
    } else {
        if (false === ($data = @file_get_contents($htaccess)))
            $this->backMsg("Cannot read .htaccess");
        if (($data != $this->get_htaccess()) && !@file_put_contents($htaccess, $data))
            $this->backMsg("Incorrect .htaccess file. Cannot rewrite it!");
    }
}

并注释掉整个部分

4) 现在您想在项目中显示 CKeditor,您必须将这些行放在相应的 php 文件/页面中,显然更改了与您的项目/应用程序相对应的变量值。但请先阅读 cmets 以决定您应该保留哪些行以及应该根据您的流程注释掉哪些行:

include_once(Absolute/Folder/path/for/CKeditor/."ckeditor/ckeditor.php") ; 

//If you did not want a session oriented way, cooment out the session related lines
$_SESSION['KCFINDER'] = array();                                    
$_SESSION['KCFINDER']['uploadURL']=$SERVER_RELATIVEPATH."userfiles/"; 

$CKEditor = new CKEditor();
$CKEditor->basePath = HTTP_COMPONENTPATH."ckeditor_3.6.2/ckeditor/";

//$_SESSION['KCFINDER']['uploadURL']="/userfiles/fashion_qr/";

$CKEditor->config["filebrowserBrowseUrl"] = ($CKEditor->basePath)."filemanagers/kcfinder_2_51/browse.php?type=files&param_project=".base64_encode(session_name());
$CKEditor->config["filebrowserImageBrowseUrl"] = ($CKEditor->basePath)."filemanagers/kcfinder_2_51/browse.php?type=images&param_project=".base64_encode(session_name());
$CKEditor->config["filebrowserFlashBrowseUrl"] = ($CKEditor->basePath)."filemanagers/kcfinder_2_51/browse.php?type=flash&param_project=".base64_encode(session_name());

$CKEditor->editor("Content", getIfSet($data['Content']));
//if you did not want a session oriented way, then in the above code code segment, just replace all the texts: base64_encode(session_name()) with this one: base64_encode(session_name($SERVER_RELATIVEPATH."userfiles/"))  

你就完成了。

【讨论】:

    【解决方案5】:

    这是我的解决方案。 CKEditor 配置:

    <?php $filesFolder = "topic123"; ?>
    CKEDITOR.replace('contentEditor', {
       filebrowserBrowseUrl: '/public/plugins/ckfinder/ckfinder.html?type=<?=$filesFolder?>'
    });
    

    我只指定了一个参数filebrowserBrowseUrl,并传递了type。现在,当您打开任何内容时,url、图像或 flash 对话框,它只会显示一个目录。

    然后在CKFinders的config.php文件中:

    $config['backends'][] = array(
        'name'         => 'topic_files',
        'adapter'      => 'local',
        'baseUrl'      => '/data/topics/',
       //'root'         => '', // Can be used to explicitly set the CKFinder user files directory.
        'chmodFiles'   => 0777,
        'chmodFolders' => 0755,
        'filesystemEncoding' => 'UTF-8',
    );
    
    if (!empty($_GET['type'])) {
        //Folder for topics
        if (preg_match("/^topic\d+$/", $_GET['type'])) {
            $config['resourceTypes'][] = [
                'name'              => $_GET['type'],
                'directory'         => $_GET['type'],
                'maxSize'           => 0,
                'allowedExtensions' => $allowedExtensions,
                'deniedExtensions'  => '',
                'backend'           => 'topic_files'
            ];
        }
    }
    

    如果文件夹(topic123)不存在,它会在你上传文件时创建它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-01
      • 1970-01-01
      • 2011-01-12
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      相关资源
      最近更新 更多