Abdo Sh
Abdo Sh

Reputation: 49

Cannot upload file, CKEditor 5 uploading image using PHP (CodeIgniter 3)

I am trying to upload images via CKEditor 5, the image is uploaded fine but CKEditor shows this error: "cannot upload file" and removes the image from the editor cannot upload file

This is my controller:

public function uploadCkeditor()
{

    $config['upload_path'] = './assets/admin/img/uploads';

    $config['allowed_types'] = 'gif|jpg|png|jpg';
    $config['max_size'] = 2000;
    $new_name = 'blog-'.date("Y-m-d").'-'.time();
    $config['file_name'] = $new_name;
    $this->load->library('upload', $config);
    if(!$this->upload->do_upload('upload')){
        echo json_encode(array('error'=> $this->upload->display_errors()));
    } else {
        $uploadData = $this->upload->data();
        echo json_encode(array('file_name'=> $uploadData['file_name']));
    }
    
}

This is CKEditor script:

    <script src="<?= base_url();?>assets/admin/js/ckeditor.js"></script>

<script>
var myEditor;

ClassicEditor.create(document.getElementById('detail'),
{
ckfinder: {
    uploadUrl: 'uploadCkeditor'
}}).then( editor => {
console.log( 'Editor was initialized', editor );
myEditor = editor;} ).catch( err => {
console.log( err );} );</script>

Upvotes: 0

Views: 1861

Answers (1)

Regentix
Regentix

Reputation: 412

I ran into the same issue, and fixed it by returning the following json response at the end of my upload handling function:

return new JsonResponse([
        'uploaded' => true,
        'fileName' => $image,
        'url' => $url
    ], 200);

It seems like you can also include an error, with a number and a message key, if needed.

Note that I ran into this issue in a Symfony project, rather than a Codeigniter project, but It should work the same way. My answer is based on this answer.

Upvotes: 3

Related Questions