


Yii combines CKEditor to implement image upload function_PHP tutorial
In a project I have been working on these days, I need to implement the image upload function in the WYSIWYG editor. I chose CKEditor because I prefer its interface. Although there is CKFinder that works well with CKEditor, its function is too complicated. After briefly looking at the documentation of CKEditor, I found that this function can be implemented by myself without resorting to CKFinder.
Although the following code is based on Yii Framework, the idea is exactly the same when using other frameworks or languages. Children's shoes in need can be referred to.
First of all, to enable the image upload function in CkEditor, you need to configure the filebrowserImageUploadUrl attribute of the editor:
CKEDITOR.replace( 'editor1',
{
filebrowserUploadUrl : '/uploader/upload.php',
filebrowserImageUploadUrl : '/uploader/upload.php?type=Images'
} );
Then implement the image upload function on the corresponding URL and return the HTML code in a specific format to CKEditor, so that CKEditor can preview and insert the image normally.
Only part of the code of the controller is intercepted below. I implemented the Controller part like this:
/**
* Save the uploaded image
*
* @return string javascript code
* @author lfyzjck
**/
public function actionImg($type, $CKEditor, $CKEditorFuncNum, $langCode = 'zh-cn')
{
if(empty($CKEditorFuncNum) | | $type != 'Images'){
$this->mkhtml($CKEditorFuncNum,'','Wrong function call');
}
if(isset($_FILES['upload '])){
//Get the image upload configuration
$options = Options::model()->findByPk(1);
$form = new UploadForm('image',$options );
$form->upload = CUploadedFile::getInstanceByName('upload');
if($form->validate()){
//File name: time + source file name
$target_filename = date('Ymd-hm',time()).$form->upload->getName();
$path = Yii::app()->basePath.' /../uploads/'.$target_filename; //Picture save path
$form->upload->saveAs($path);
$this->mkhtml($CKEditorFuncNum,Yii:: app()->baseUrl.'/uploads/'.$target_filename, "Upload successful");
}
else{
$this->mkhtml($CKEditorFuncNum,'',$form ->getError('upload'));
}
}
}
/**
* Return the prompt message of CKEditor
*
* @return void
* @author lfyzjck
**/
private function mkhtml($fn, $fileurl, $message)
{
$str = '';
exit($str);
}
The mkhtml function that needs special explanation will call the CKEditor function to generate prompt information. When the upload is successful, the image link will be returned, and CKEditor will generate an image preview based on the URL.
Then comes the UploadForm code, which will verify whether the format and size of the image meet the requirements.
class UploadForm extends CFormModel
{
public $upload;
private $options;
private $type;
public function __construct($type, $options){
$this->options = $options;
$this->type = $type;
}
/**
* Declares the validation rules.
* The rules state that username and password are required,
* and password needs to be authenticated.
*/
public function rules()
{
return array(
array('upload', 'file',
'types' => $this-> options->getAttribute("allow_".$this->type."_type"),
'maxSize' => 1024 * (int)$this->options->getAttribute("allow_" .$this->type."_maxsize"),
'tooLarge'=>'File size exceeds limit',
),
);
}
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











With the widespread use of web applications, creating rich text editors has become more and more common. CKEditor is widely recognized as one of the best rich text editors because of its good customizability and ease of use. This article will introduce how to create a rich text editor using PHP and CKEditor. Introduction to CKEditor CKEditor is an open source, cross-platform rich text editor implemented through JavaScript. It provides an intuitive and easy-to-understand toolbar, including font style, formatting, graphics, etc.

Steps to implement image upload and display using CakePHP framework Introduction: In modern web applications, image upload and display are common functional requirements. The CakePHP framework provides developers with powerful functions and convenient tools, making it simple and efficient to upload and display images. This article will introduce you to how to use the CakePHP framework to upload and display images. Step 1: Create a file upload form First, we need to create a form in the view file for users to upload images. The following is an example of

WeChat applet implements picture upload function With the development of mobile Internet, WeChat applet has become an indispensable part of people's lives. WeChat mini programs not only provide a wealth of application scenarios, but also support developer-defined functions, including image upload functions. This article will introduce how to implement the image upload function in the WeChat applet and provide specific code examples. 1. Preparatory work Before starting to write code, we need to download and install the WeChat developer tools and register as a WeChat developer. At the same time, you also need to understand WeChat

How to handle image uploading and compression in Vue technology development In modern web applications, image uploading is a very common requirement. However, due to network transmission and storage reasons, directly uploading original high-resolution images may result in slow upload speeds and a large waste of storage space. Therefore, uploading and compressing images is very important. In Vue technology development, we can use some ready-made solutions to handle image uploading and compression. The following will introduce how to use vue-upload-comone

In the current information age, big data, artificial intelligence, cloud computing and other technologies have become the focus of major enterprises. Among these technologies, graphics card rendering technology, as a high-performance graphics processing technology, has received more and more attention. Graphics card rendering technology is widely used in game development, film and television special effects, engineering modeling and other fields. For developers, choosing a framework that suits their projects is a very important decision. Among current languages, PHP is a very dynamic language. Some excellent PHP frameworks such as Yii2, Ph

How to use PHP and Vue to implement the image upload function. In modern web development, the image upload function is a very common requirement. This article will introduce in detail how to use PHP and Vue to implement the image upload function, and provide specific code examples. 1. Front-end part (Vue) First, you need to create a form for uploading images on the front-end. The specific code is as follows:<template><div><inputtype="fil

With the development of the Internet, image uploading has become an essential feature in website and application development. In the field of PHP, ThinkPHP6 has become a very popular development framework. In this article, we will introduce how to use ThinkPHP6 to implement image upload. 1. Create project and controller First, we need to create a new ThinkPHP6 project. You can use Composer to install it, or you can download the latest version from the official website. After the installation is complete, enter in the console

With the continuous development of cloud computing technology, data backup has become something that every enterprise must do. In this context, it is particularly important to develop a highly available cloud backup system. The PHP framework Yii is a powerful framework that can help developers quickly build high-performance web applications. The following will introduce how to use the Yii framework to develop a highly available cloud backup system. Designing the database model In the Yii framework, the database model is a very important part. Because the data backup system requires a lot of tables and relationships
