Ckeditor + ckfinder Image Upload configuration (C #/asp.net/php)

Source: Internet
Author: User
Ckeditor + ckfinder Image Upload configuration (C #/asp.net/php)

Source and network

Ckeditor + ckfinder Image Upload configuration (C #/asp tutorial. Net/PHP tutorial)

PHP

All the code of keditor is rewritten, but the upload function is not available in it. It is a pure online file editor. to upload images, download ckfinder.

First download the source code from the official website and decompress it in the website root directory (the default ckeditor and ckfinder folders do not need to be changed)

Insert JS on the required page

<SCRIPT type = "text/Webpage effects" src = "/ckeditor. js"> </SCRIPT>

<SCRIPT type = "text/JavaScript" src = "/ckfinder. js"> </SCRIPT>

<Form action = "sroan. php" method = "post">

<Textarea Cols = "80" id = "editor1" name = "editor1" rows = "10"> </textarea>

<Input type = "Submit" value = "Submit"/>

<SCRIPT type = "text/JavaScript">
VaR editor = ckeditor. Replace ('time ');
Ckfinder. setupckeditor (Editor, '../ckfinder/'); // relative path of the total ckfinder directory.
</SCRIPT>

Modify the checkauthentication function in ckfinder/config. php and change the return value to true (fasle by default)

This enables the upload function.

If the above method does not work, you can try it.

1. Download and install ckeditor:
Http://ckeditor.com/
Decompress the downloaded ckeditor and place it in the website path.
2. Download and install ckfinder:
Http://ckfinder.com/download
Decompress the downloaded ckfinder and put it in the same directory as ckeditor.

3. Use ckeditor and ckfinder on the webpage:
Ckeditor actually replaces a textarea tag, so put textarea in a form. when submitted to the PHP server, use $ _ Get ['xxx'] or $ _ post ['xxx'] to obtain the edited data. Note that because ckeditor must replace textarea, the corresponding JavaScript code "ckeditor. Replace (xxxxxx)" should be placed behind textarea.
Simply create a new test.html file (in the same directory as ckeditor and ckfinder) and use the following example to modify it. In the browser, view test.html and you will be able to see ckeditor on the webpage.




Ckeditor





4. Configure ckfinder to upload images and Flash files.

Click "image". The upload button is displayed in the "Upload" tab in the pop-up window, but the upload fails. Because ckfinder is not configured yet. You need to create the directory of the uploaded file and modify the three places in ckfinder/config. php:
A. Create a directory to save the uploaded files, such as upload.
(Put the upload folder in the same level as ckeditor and ckfinder. All three directories are in the project directory)
The path is/upload/
[For Linux: Set the permission to be read and written by the PHP server. The simplest thing is that the PHP server has the permission to store files in the CHMOD 777 upload.]

B. Locate row 32nd of the configuration file (ckfinder/config. php) and set function checkauthentication () {return false ;}
Change to function checkauthentication () {return true ;}

C. Find row 63rd and change the value of $ baseurl to the URL that saves the directory of the uploaded file,
For example, $ baseurl = '/project name/upload/'; (the address here is the absolute path starting from the project directory, and it is determined that the upload directory already exists)

D. find row 82nd and delete $ basedir = resolveurl ($ baseurl). Modify $ basedir to the absolute path of the file directory to be uploaded. (During local testing, you must start from the disk and directory, that is, the direct directory of C:/or D)
For example, $ basedir = '/apache2/htdocs/CK/upload/'; (apache2 is a direct directory under D)
This is because the resolveurl ($ baseurl) function does not work properly.

Now, you can use ckeditor and ckfinder to upload files.

For the server B. PHP code in the above small example:


Header ("Content-Type: text/html; charset = UTF-8 ");
$ STR = $ _ post ['editor1'];
Echo $ STR;
?>

As you can see, after clicking the submit button, the server receives the content in ckeditor and uses echo output to generate a content exactly the same as that edited in ckeditor. On the server side, you can save the received content to the database tutorial. Then, read and display it on the corresponding page.

Bytes ---------------------------------------------------------------------------------------------------------

The FCKeditor upload image contains a function that allows you to browse the server directory. If you are in the background, you can use this function. If you are a front-end user, do not use this function.
However, viewing images is not very convenient. ckfinder is very simple.
FCKeditor is prepared as follows:
Modify file:
Fckeditoreditorfilemanagerconnectorsphpconfig. php

// Change $ config ['enabled'] to true to allow upload.

 

$ Config

[

'Enabled'

]

=

True

;

 

// $ Config ['userfilespath'] setting the upload directory relative to the root directory is useless at present, because the actual upload is in the following preparation

 

$ Config

[

'Userfilespath'

]

=

"/Upload /"

;

 

// $ Config ['userfilesabsolutepath'] specifies the location of the uploaded image, including the root directory.

 

$ Config

[

'Userfilesabsolutepath'

]

=

'/Public/upload /'

;

 

 

 

Set ckfinder config. php

$ Baseurl

=

'/Upload /'

;

 

// Change checkauthentication to return true. Here you can add your own permission judgment.

 

Function

Checkauthentication (

)

 

{

 

Return

True

;

 

}

Use in program code

$ FCKeditor

=

New

FCKeditor (

$ Fckname

)

;

 

$ FCKeditor

->

Basepath

=

'/JS/FCKeditor /'

;

 

// $ Toolbar_set set default value of the toolbar: Default

 

$ FCKeditor

->

Toolbarset

=

$ Toolbar_set

;

 

// $ Width

 

$ FCKeditor

->

Width

=

$ Width

;

 

// $ Height

 

$ FCKeditor

->

Height

=

$ Height

;

 

// Path of the skin file

 

$ FCKeditor

->

Config

=

Array

(

'Skinpath'

=>

$ Skinpath

)

;

 

$ FCKeditor

->

Value

=

$ Value

;

 

// Use ckfinder before fckedior

 

Ckfinder ::

Setupfckeditor

(

$ FCKeditor

,

"/JS/ckfinder /"

)

;

 

$ Fckhtml

=

$ FCKeditor

->

Create

(

)

;

The detailed C # configuration method is as follows:

The latest versions of ckeditor and ckfinder can be downloaded from the official website (http://cksource.com.
Put the above two resources in the root directory of the Website:/ckeditor and/ckfinder (case-insensitive)
Use ckeditor on the page:
<Textarea Cols = "80" id = "prcontent" name = "content" rows = "50"> Hello world! </Textarea>
<SCRIPT type = "text/JavaScript">
Ckeditor. Replace ('content', {Height: 200, width: 520 });
</SCRIPT>
Ckeditor does not provide the upload function. Therefore, you must use ckfinder to upload files.
Step 1: The website must reference the ckfinder DLL (directory:/ckfinder/bin/release/ckfinder. dll)
Step 2: Configure ckeditor config. js (directory:/ckeditor/config. JS) and add it to the ckeditor. editorconfig function. You can remove unnecessary functions.
Config. filebrowserbrowseurl = '/ckfinder/ckfinder.html'; // browse service folders when uploading files
Config. filebrowserimagebrowseurl = '/ckfinder/ckfinder.html? Type = images; // browse the service folder when uploading images
Config. filebrowserflashbrowseurl = '/ckfinder/ckfinder.html? Type = flash '; // browse service folder when uploading flash
Config. filebrowseruploadurl = '/ckfinder/CORE/connector/aspx/connector. aspx? Command = quickupload & type = files '; // Upload File button (TAG)
Config. filebrowserimageuploadurl = '/ckfinder/CORE/connector/aspx/connector. aspx? Command = quickupload & type = images '; // upload image button (TAG)
Config. filebrowserflashuploadurl = '/ckfinder/CORE/connector/aspx/connector. aspx? Command = quickupload & type = flash '; // upload the flash button (TAG)
After the configuration is complete, ckeditor provides the upload function. However, if you want to change the image file name to a random file name when uploading an image, what should you do? Next, let's take a look at Step 3.

Step 3: Modify the ckfinder source code. Ckfinder comes with source code, directory:/ckfinde/_ Source
Create a new project in vs pointing to ckfinde/_ source/ckfinder. net. sln
1) Open the/settings/configfile. CS file.
Locate 27 rows and add an attribute: Public bool randomrename; // rename randomly
Locate 67 rows and assign the value to the attribute: randomrename = true; // The default value is true.
Save closed files
2) Open the/connector/config. CS file.
Locate row 62 and add an attribute:
Public bool randomrename
{
Get {return settings. configfile. Current. randomrename ;}
}
Save closed files
3) Open the/connector/commandhandlers/fileuploadcommandhandler. CS file.
Locate 64 rows and add a judgment code:
If (config. Current. randomrename) // use a random name
Sfilename = datetime. Now. tostring ("yyyymmddhhmmssfff") + "." + sextension;

Save closed files
4) regenerate the project and run/ckfinder/_ source/bin/debug/ckfinder. DLL overwrite/ckfinder/bin/release/ckfinder. DLL or website project remove the reference previously added and then re-import from ckfinder.. Net project DLL
Last step: Open/ckfinder/config. ascx
Locate 42 rows and add a property: (in fact, this addition can be done without adding, because the default value is set before, but it must be set to false when using the original name)
// Use random file names after uploading
Randomrename = true;

Save close
Now that the configuration is successful, ckeditor has the upload function. Other function settings are available for further research ~
Note: The ckeditor and ckfinder folders have many unnecessary things, such as the ones whose names are prefixed with underscores, And the. Net project does not require PHP or ASP.
If an error occurs during file uploading: the file cannot be browsed due to security reasons, contact the system administrator and check the ckfinder configuration file.
You need to modify the config. ascx File
Public override bool checkauthentication ()
{
Reture false;
}
To:
Public override bool checkauthentication ()
{
// Used for Form Verification
Return request. isauthenticated;
// Reture true; not recommended
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.