KCFinder与CKEditor - 为上传文件设置动态文件夹

时间:2012-04-18 14:23:35

标签: php image file-upload

我正在使用CCFditor和KCFinder - 我想根据动态值设置个人上传文件。

我想在用户登录后根据SESSION值动态更改用户可以上传的文件夹。

登录后,我想限制用户可以通过KCFinder插件访问的文件夹 所以,例如..

User abc their path would be abc/images
User foo their path would be foo/images
User abc cannot see foo's images & vice-versa

2 个答案:

答案 0 :(得分:5)

我遇到了同样的问题并且让事情变得更糟,这取决于谁登录,管理员或标准会员。

当然,我把它整合到了TinyMCE中,但我认为原理是相同的

以下是我对问题的解决方法

我的文件夹结构是

/my_uploads/media

/my_uploads/media/member_folder
  1. 初始化config.php文件顶部的php会话

    在session_start()

  2. 编辑'types'=>数组并注释掉所有不同的“类型”

    // CKEditor & FCKEditor types
    // 'files'   =>  "",
    // 'flash'   =>  "swf",
    // 'images'  =>  "*img",
    
    // TinyMCE types
    // 'files'    =>  "",
    // 'media'   =>  "swf flv avi mpg mpeg qt mov wmv asf rm",
     // 'image'   =>  "*img",
    
  3. 将以下'type'添加到数组

    $ _ SESSION ['fold_type'] => “* img swf flv avi mpg mpeg qt mov wmv asf rm”,

  4. 保存您的config.php文件

  5. 打开初始化编辑器的应用程序页面并调用KCFinder

  6. 在初始化编辑器之前,添加以下行,声明各种KCFinder变量

    $_SESSION['KCFINDER'] = array();
    $_SESSION['KCFINDER']['disabled'] = false; // Activate the uploader,
    $_SESSION['KCFINDER']['uploadURL'] = "/uploads"; 
    $_SESSION['fold_type'] = "media"; 
    
  7.     $_SESSION['KCFINDER'] = array();
        $_SESSION['KCFINDER']['disabled'] = false; // Activate the uploader,
        $_SESSION['KCFINDER']['uploadURL'] = "/my_uploads/media/Members";
        $_SESSION['fold_type'] = "member_account_number_pulled_from_secure_session_variable";
    

    7将您调用上传者的行留在此处;

       file: '../tiny_mce/plugins/kcfinder/browse.php?opener=tinymce',
    

    Set upload folder dynamically for KCFinder

答案 1 :(得分:1)

可以通过多种方式完成。我正在解释一个过程,我根据我的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/"))  

你已经完成了。

相关问题