_php example of image uploading function with Yii combined with CKEditor

Source: Internet
Author: User
Tags yii

These days to do a project in the WYSIWYG editor to achieve the image upload function, I prefer the CKEditor interface and chose it. Although have with CKEditor with good ckfinder, but this dongdong function is too complex, a simple look at the Ckedtior document, found that this function can be achieved without the help of Ckfinder.

Although the following code is based on the YII framework, but with other frameworks or language ideas are exactly the same, the need for children's shoes can be referred to.

First of all, to let ckeditor appear image upload function, you need to configure the editor's Filebrowserimageuploadurl properties:

Copy Code code as follows:

Ckeditor.replace (' Editor1 ',
{
Filebrowseruploadurl: '/uploader/upload.php ',
Filebrowserimageuploadurl: '/uploader/upload.php?type=images '
});

Then the corresponding URL on the implementation of the image upload function, and to ckeditor return a specific format of the HTML code, CKEditor will be able to preview and insert the picture.
Only part of the controller's code is intercepted below, as I did in the controller section:
Copy Code code as follows:

/**
* Save uploaded pictures
*
* @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 information about picture upload configuration
$options = Options::model ()->findbypk (1);
$form = new Uploadform (' image ', $options);
$form->upload = cuploadedfile::getinstancebyname (' upload ');
if ($form->validate ()) {
File name: Time + source filename
$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 success");
}
else{
$this->mkhtml ($CKEditorFuncNum, ', $form->geterror (' upload '));
}
}
}
/**
* Return the CKEditor message
*
* @return void
* @author Lfyzjck
**/
Private Function mkhtml ($FN, $fileurl, $message)
{
$str = ';
Exit ($STR);
}

A mkhtml function that requires special instructions, and he invokes the CKEditor function to produce a hint. When the upload is successful, return the picture link, CKEditor will generate a picture preview based on the URL.

Then there is the Uploadform code, which verifies that the format and size of the picture match the requirements.

Copy Code code as follows:

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 so 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 ',
   ,
  );
&NBSP}
}

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.