Ckeditor 5 and Image Upload in PHP

Hello,

Today I want to share insight and a little snippet on how to integrate the latest version of CKeditor (V5) and a serverside image upload in PHP.

The CKeditor 5 requires a custom upload adapter for the upload process.

Prerequisite knowledge of PHP upload and Javascript is required.

JAVASCRIPT

class MyUploadAdapter {
    constructor( loader ) {
        // CKEditor 5's FileLoader instance.
        this.loader = loader;

        // URL where to send files.
        this.url = 'https://example.com/upload';
    }

    // Starts the upload process.
    upload() {
        return this.loader.file
            .then( file => new Promise( ( resolve, reject ) => {
                this._initRequest();
                this._initListeners( resolve, reject, file );
                this._sendRequest( file );
            } ) );
    }

    // Aborts the upload process.
    abort() {
        if ( this.xhr ) {
            this.xhr.abort();
        }
    }


    _initRequest() {
        const xhr = this.xhr = new XMLHttpRequest();

        xhr.open( 'POST', this.url ,true);
        xhr.responseType = 'json';
    }


    _initListeners( resolve, reject,file ) {
        const xhr = this.xhr;
        const loader = this.loader;
        const genericErrorText = 'Couldn\'t upload file:' + ` ${ file.name }.`;

        xhr.addEventListener( 'error', () => reject( genericErrorText ) );
        xhr.addEventListener( 'abort', () => reject() );
        xhr.addEventListener( 'load', () => {
            const response = xhr.response;

            if ( !response || response.error ) {
                return reject( response && response.error ? response.error.message : genericErrorText );
            }
            //console.log(response);
            // If the upload is successful, resolve the upload promise with an object containing
            // at least the "default" URL, pointing to the image on the server.
            resolve( {
                default: response.url
            } );
        } );

        if ( xhr.upload ) {
            xhr.upload.addEventListener( 'progress', evt => {
                if ( evt.lengthComputable ) {
                    loader.uploadTotal = evt.total;
                    loader.uploaded = evt.loaded;
                }
            } );
        }
    }

    // Prepares the data and sends the request.
    _sendRequest(file) {
        const data = new FormData();
        //console.log(this.loader.file);
        data.append('upload', file );
    //csrf_token CSRF protection
        //data.append('csrf_token', requestToken);

        this.xhr.send( data );
    }
}
function MyCustomUploadAdapterPlugin( editor ) {
    editor.plugins.get( 'FileRepository' ).createUploadAdapter = ( loader ) => {
        return new MyUploadAdapter( loader );
    };
}

function init_ckeditor(selector,timeout){
    ClassicEditor
        .create( document.querySelector(selector ),{
            extraPlugins: [ MyCustomUploadAdapterPlugin ],
        } )
        .then( editor => {
            console.log( editor );
        } )
        .catch( error => {
            console.error( error );
        } );
}

PHP

The file will be available in the $_FILES Global array with key 'upload' Afterward, you can return

<?php 
file_upload_page(){
    //process the image upload 
    //$url is the absolute path to the file you upload
    return json_encode(array('url'=>$url))
}
?>

CONCLUSION

With the above snippets, you can easily integrate CKEditor 5 and file upload in PHP I hope I was able to save you some development time.

Happy Coding Oluwaseun

REFERENCES

Ckeditor Docs