Home >Backend Development >PHP Tutorial >CKEditor integrates ckfinder to implement image upload_PHP tutorial

CKEditor integrates ckfinder to implement image upload_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:11:341093browse

After working on it for a long time, I finally made it clear about CKEditor and cooperated with ckfinder to upload pictures. The specific steps are provided to those who need it. 1. Create the project ckeditor, decompress ckeditor and ckfinder and put them under the project

The download addresses of the two items are:

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

ckfinder: http://ckfinder.com/download

The project directory structure is:

ckeditor

---ckeditor

-- ckfinder

--test.php PHP file used for testing

2. Find the config.php file in the ckfinder directory and open it

1) Locate the CheckAuthentication method, default This method only returns the value false, which does not allow files to be uploaded to the server. It needs to be modified. The function has been marked in English. It is not recommended to directly change it to false, because it will be unsafe and everything will be uploaded, so Use session to process it, and it will look like the following

The code is as follows
 代码如下 复制代码

session_start();

function CheckAuthentication()

    if(isset($_SESSION['UseEditor']))//这个随便命名就可以了

    return true;

    else

    return false;

}

Copy code

session_start();

 代码如下 复制代码

$baseUrl = '/ckeditor/upfiles/';

function CheckAuthentication()

{

if(isset( $_SESSION['UseEditor']))//You can name this whatever you want

return true;
 代码如下 复制代码

$sUnsafeFileName = CKFinder_Connector_Utils_FileSystem::convertToFilesystemEncoding(CKFinder_Connector_Utils_Misc::mbBasename($uploadedFile['name']));

//先获取原文件后缀

$sExtension=CKFinder_Connector_Utils_FileSystem::getExtension($sUnsafeFileName);

//重新命名文件

$sUnsafeFileName=date('YmdHis').'.'.$sExtension;

else

return false;

 代码如下 复制代码

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", "

Initial value.

");

}
2) Configure the location of the uploaded file. By default, ckfinder will place the uploaded file in the ckfinder/userfiles/images folder in the root directory. We can Modify it to your own project directory, create the upfiles folder under the project, find baseUrl in config.php and modify it to:
The code is as follows Copy code
$baseUrl = '/ckeditor/upfiles/'; In this way, the uploaded file will be saved to the images folder under upfiles. Of course, you can also set different upload folders according to your needs. As for how to pass the value to him to determine the upload folder, of course it is through the session. 3. Modify the name of the uploaded file. By default, the system names it with the original name, so we rename the file. The method is: open the FileUpload.php file under ckfindercoreconnectorphpphp5CommandHandler Find $sUnsafeFileName followed by the rename code, as follows:
The code is as follows Copy code
$sUnsafeFileName = CKFinder_Connector_Utils_FileSystem::convertToFilesystemEncoding(CKFinder_Connector_Utils_Misc::mbBasename($uploadedFile ['name']));//Get the original file suffix first$sExtension=CKFinder_Connector_Utils_FileSystem::getExtension($sUnsafeFileName);//Rename the file $sUnsafeFileName=date('YmdHis').'.'.$sExtension; 4. Finally in test. Use ckfinder in php, the code is as follows:
The code is as follows Copy code
session_start();$_SESSION['UseEditor']='ok';//Don't forget this, otherwise you won't be able to uploadinclude_once('ckeditor/ckeditor.php'); include_once('ckfinder/ckfinder.php'); $fc = new CKEditor(); CKFinder::SetupCKEditor($fc,'./ckfinder/');$fc->editor("content", "

Initial value.

" );

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444645.htmlTechArticleAfter working on it for a long time, I finally figured out CKEditor and cooperated with ckfinder to upload pictures. The specific steps are given as needed. Comrades. 1. Create the project ckeditor, and unzip ckeditor and ckfinder...
Statement:
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