Home > php教程 > php手册 > CKEditor整合ckfinder实现图片上传

CKEditor整合ckfinder实现图片上传

WBOY
Release: 2016-05-25 16:39:43
Original
1096 people have browsed it

搞了半天终于把CKEditor搞了个明明白白,配合ckfinder实现了图片的上传,具体步骤给需要的同志们.

1、建立项目ckeditor,将ckeditor和ckfinder解压缩放到项目下,两个东东的下载地址分别为:

ckeditor:http://ckeditor.com/download/

ckfinder:http://ckfinder.com/download

项目目录结构为:

ckeditor

---ckeditor

--ckfinder

--test.php 用来测试的PHP文件

2、找到ckfinder目录下的config.php文件打开

1)定位到CheckAuthentication方法,默认此方法只有返回值false,这样是不允许上传文件到服务器的,需要将其进行修改,函数内已经用英文注明,不建议直接改为false,因为那样会不安全,什么都会上传了,所以用session来处理一下,就成了下面的样子,代码如下:

session_start(); 
function CheckAuthentication() 
{  
    if(isset($_SESSION['UseEditor']))//这个随便命名就可以了 
    return true; 
    else 
    return false; 
}
Copy after login

2) 配置上传文件位置.

默认ckfinder会把上传文件放到根目录下的ckfinder/userfiles/images文件夹下,我们可以修改成自己的项目目录下,在项目下建立upfiles文件夹,在config.php中找到baseUrl并且修改为如下代码:

$baseUrl = '/ckeditor/upfiles/';
Copy after login

这样上传文件会保存到upfiles下的images文件夹,当然,你也可以根据需要设置不同的上传文件夹,至于怎么传值给他以判断上传文件夹,当然是通过session了.

3、修改上传文件名,默认系统以原有名字命名,所以我们重新命名文件,方法为:打开ckfinder/coreconnectorphp/php5/CommandHandler下FileUpload.php文件,找到 $sUnsafeFileName 后面加上重命名代码,如下:

$sUnsafeFileName = CKFinder_Connector_Utils_FileSystem::convertToFilesystemEncoding(CKFinder_Connector_Utils_Misc::mbBasename($uploadedFile['name'])); 
//先获取原文件后缀 
$sExtension=CKFinder_Connector_Utils_FileSystem::getExtension($sUnsafeFileName); 
//重新命名文件 
$sUnsafeFileName=date('YmdHis').'.'.$sExtension;
Copy after login

4、最后在test.php中使用ckfinder,代码如下:

session_start(); 
$_SESSION['UseEditor']='ok';//一定别忘记这个哦,否则没法上传 
include_once('ckeditor/ckeditor.php'); 
include_once('ckfinder/ckfinder.php'); 
$fc = new CKEditor(); 
CKFinder::SetupCKEditor($fc,'./ckfinder/'); 
$fc->editor("content", "<p>Initial value.</p>");
Copy after login


本文地址:

转载随意,但请附上文章地址:-)

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template