如何在KCFinder中更改图像的默认浏览目录?

时间:2011-12-14 11:45:34

标签: php javascript ckeditor

我正在使用CCFditor和KCFinder。除了一件事,一切都很好。

我的默认上传网址是:

'uploadURL' => "/userfiles"

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

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

我在http://kcfinder.sunhater.com

上找不到任何内容

5 个答案:

答案 0 :(得分:2)

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

当您在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"
    ),

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

答案 1 :(得分:1)

我遇到了同样的问题,直到我尝试了以下会话参数(动态设置; 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';

希望这有帮助!

答案 2 :(得分:0)

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';

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

答案 3 :(得分:0)

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

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

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

//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文件/页面中,显然改变了与你的项目/ app相对应的变量值。但请首先阅读评论,以确定应保留哪些行以及根据您的流程注释的内容:

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/"))  

你已经完成了。

答案 4 :(得分:0)

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

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

我只指定了一个参数 filebrowserBrowseUrl 并传递了类型。现在,当您打开任何内容,网址,图片或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)不存在,它将在您上传文件时创建它。