Adding Upload Functionality to CKEditor

1. First, modify the config.js configuration file in the CKEditor directory

 

CKEDITOR.editorConfig = function( config )
{
var siteUrl = 'http://'+document.domain+'/';
    config.filebrowserImageUploadUrl = siteUrl+'admin/ckeditor/upload.php?type=img';// Path to upload.php file, provided below
    config.filebrowserFlashUploadUrl = siteUrl+'admin/ckeditor/upload.php?type=flash';

};

2. upload.php file, handles the upload

<?php

$errRequestFunction = "Error as a function call requests"; // Error: invalid function call request
$errRequestFile = "Request the wrong file called"; // Error: invalid file call request
$errFileType = "The wrong file type"; // Error: incorrect file type!
$errUpfile = "File upload failed, please check the upload directory and directory read and write permissions set"; // Error: file upload failed, please check the upload directory settings and read/write permissions
$uploadExceed = "The uploaded file can not exceed"; // Uploaded file cannot exceed
$config = array ();
$config ['type'] = array ("flash", "img" ); // Allowed upload type values
$config ['img'] = array ("jpg", "bmp", "gif" ); // Allowed image file extensions
$config ['flash'] = array ("flv", "swf" ); // Allowed flash file extensions
$config ['flash_size'] = 2000; // Maximum flash upload size, unit: KB
$config ['img_size'] = 5000; // Maximum image upload size, unit: KB
$config ['message'] = "Upload Success!"; // Message displayed on successful upload; if empty, no message is shown after upload success
$config ['name'] = mktime (); // Naming rule for uploaded files, using Unix timestamp here
$config ['flash_dir'] = $_SERVER['DOCUMENT_ROOT']."/upload"; // Upload directory for flash files (absolute path), convenient for placing upload.php anywhere on the site; no trailing "/". Uploaded files will be placed in the upload folder under the site root
$config ['img_dir'] = $_SERVER['DOCUMENT_ROOT']."/upload"; // Upload directory for image files (absolute path), convenient for placing upload.php anywhere on the site; no trailing "/". Uploaded files will be placed in the upload folder under the site root
$config ['site_url'] = "http://www.xxx.com"; // Website URL, related to the URL of uploaded images; no trailing "/"; can be left empty
// Create folders if they do not exist
isDirAll($config ['flash_dir']);
isDirAll($config ['img_dir']);
// File upload
uploadfile ();
function uploadfile() {
    global $config;
    // Check if this is an illegal call
    if (empty ( $_GET ['CKEditorFuncNum'] ))
        mkhtml ( 1, "", $errRequestFunction );
    $fn = $_GET ['CKEditorFuncNum'];
    if (! in_array ( $_GET ['type'], $config ['type'] ))
        mkhtml ( 1, "", $errRequestFile );
    $type = $_GET ['type'];
    if (is_uploaded_file ( $_FILES ['upload'] ['tmp_name'] )) {
        // Check if the uploaded file type is allowed
        $filearr = pathinfo ( $_FILES ['upload'] ['name'] );
        $filetype = $filearr ["extension"];
        if (! in_array ( $filetype, $config [$type] ))
            mkhtml ( $fn, "", $errFileType );
            // Check if the file size meets the requirements
        if ($_FILES ['upload'] ['size'] > $config [$type . "_size"] * 1024)
            mkhtml ( $fn, "", $uploadExceed . $config [$type . "_size"] . "KB!" );
        $file_abso = $config [$type . "_dir"] . "/" . $config ['name'] . "." . $filetype;
        $file_host = $file_abso;
        $file_path = "/upload/" . $config ['name'] . "." . $filetype;// Adjust the /upload/ path here to match your upload directory
        if (move_uploaded_file ( $_FILES ['upload'] ['tmp_name'], $file_host )) {
            mkhtml ( $fn, $config ['site_url'] . $file_path, $config ['message'] );
        } else {
            mkhtml ( $fn, "", $errUpfile . $file_host );
        }
    }
}
// Output JS callback
function mkhtml($fn, $fileurl, $message) {
    $str = '<script type="text/javascript">window.parent.CKEDITOR.tools.callFunction(' . $fn . ', '' . $fileurl . '', '' . $message . '');</script>';
    exit ( $str );
}

function isDirAll($dir) {
    if (! is_dir ( $dir )) {
        $dirArr = explode ( "/", $dir );
        $mkDir = "";
        for($i = 0; $i < count ( $dirArr ); $i ++) {
            if ($dirArr [$i] != "") {
                $mkDir .= '/' . $dirArr [$i];
                isMkDir ( $mkDir );
            }
        }
    }
}

function isMkDir($dir = "") {
    if (! is_dir ( $dir )) {
        @mkdir ( $dir, 0777 );
    }
}
?>

Leave a Comment

Your email address will not be published.