CKEditor 图片上传

可以做如下配置:

CKEDITOR.replace('editor1',{
    filebrowserBrowseUrl:'/browser/browse.php',
    filebrowserUploadUrl:'/uploader/upload.php'});


It is also possible to set a separate URL for a selected dialog window by using the dialog window name in file browser settings: filebrowserBrowseUrland filebrowserUploadUrl.

CKEDITOR.replace('editor1',{
    filebrowserBrowseUrl:'/browser/browse.php',
    filebrowserImageBrowseUrl:'/browser/browse.php?type=Images',
    filebrowserUploadUrl:'/uploader/upload.php',
    filebrowserImageUploadUrl:'/uploader/upload.php?type=Images'});


In the example above, the 
filebrowserBrowseUrl and filebrowserUploadUrl settings will be used by default. In the Image Properties dialog window CKEditor will use the filebrowserImageBrowseUrl and filebrowserImageUploadUrl configuration settings instead.
在image上传中,会使用filebrowserImageUploadUrl;


传递选择的文件:
To send back the file URL from an external file browser, call CKEDITOR.tools.callFunction and pass CKEditorFuncNum as the first argument:
window.opener.CKEDITOR.tools.callFunction( funcNum, fileUrl [, data]);

If data (the third argument) is a string, it will be displayed by CKEditor. This parameter is usually used to display an error message if a problem occurs during the file upload.


The following code shows how to send back the URL of an uploaded file from the PHP connector:

<?php
    // Required: anonymous function reference number as explained above.
    $funcNum = $_GET['CKEditorFuncNum'] ;
    // Optional: instance name (might be used to load a specific configuration file or anything else).
    $CKEditor = $_GET['CKEditor'] ;
    // Optional: might be used to provide localized messages.
    $langCode = $_GET['langCode'] ;

    // Check the $_FILES array and save the file. Assign the correct path to a variable ($url).
    $url = '/path/to/uploaded/file.ext';
    // Usually you will only assign something here if the file could not be uploaded.
    $message = ;

    echo "<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction($funcNum, '$url', '$message');</script>";
?>

完整的上传图片的代码:

在config设置 

filebrowserImageUploadUrl: 'uploadImage.php?type=Images',

uploadImage.php代码如下:

<?php
$url = 'images/uploads/'.time()."_".$_FILES['upload']['name'];

/*
$move = @ move_uploaded_file($_FILES['upload']['tmp_name'], $url);
$funcNum = $_GET['CKEditorFuncNum'] ;
echo "<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction($funcNum, '$url', '$message');</script>";
*/
 //extensive suitability check before doing anything with the file...
    if (($_FILES['upload'] == "none") ||(empty($_FILES['upload']['name'])) )
    {
       $message = "No file uploaded.";
    }
    else if ($_FILES['upload']["size"] == 0)
    {
       $message = "The file is of zero length.";
    }
    else if (($_FILES['upload']["type"] != "image/pjpeg") && ($_FILES['upload']["type"] != "image/jpeg") && ($_FILES['upload']["type"] != "image/png")&&($_FILES['upload']["type"] != "image/gif"))
    {
       $message = "图片格式必须是jpg/gif/png";
    }
    else if (!is_uploaded_file($_FILES['upload']["tmp_name"]))
    {
       $message = "You may be attempting to hack our server. We're on to you; expect a knock on the door sometime soon.";
    }
    else {
      $message = "";
      $move = @ move_uploaded_file($_FILES['upload']['tmp_name'], $url);
      if(!$move)
      {
         $message = "Error moving uploaded file. Check the script is granted Read/Write/Modify permissions.";
      }
      //$url=$url;
    }
 
$funcNum = $_GET['CKEditorFuncNum'] ;
echo "<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction($funcNum, '$url', '$message');</script>";
?>

参考:

 http://www.caeus.com/articles/how-to-add-and-upload-an-image-using-ckeditor/

http://www.phpzixue.cn/detail847.shtml

http://blog.csdn.net/xiao__gui/article/details/7684505

http://codeigniter.org.cn/forums/thread-13708-1-1.html

原文地址:https://www.cnblogs.com/youxin/p/3150549.html