简体   繁体   中英

How do I integrate codeigniter with redactor js image upload (wysiwyg)?

I'm trying to use Codeigniter with a wysiwyg editor called redactor js. Basically, what I'm trying to accomplish can be found here: http://imperavi.com/redactor/docs/images/

It seems really easy but I can't get it to work. I keep getting a 500 error in my console. Here's my current coding:

Redactor js:

    <script type="text/javascript">
        $(document).ready(

        function() {
            $('#redactor_content').redactor({
                imageUpload: 'user/simple_upload'
            });
        });
    </script>

PHP class that handles the uploads:

class User extends MX_Controller
{
    public function simple_upload()
    {
        $dir = './uploads/user_post_uploads/';

        $_FILES['file']['type'] = strtolower($_FILES['file']['type']);

        if ($_FILES['file']['type'] == 'image/png' || $_FILES['file']['type'] ==
            'image/jpg' || $_FILES['file']['type'] == 'image/gif' || $_FILES['file']['type'] ==
            'image/jpeg' || $_FILES['file']['type'] == 'image/pjpeg') {
            // setting file's mysterious name
            $filename = md5(date('YmdHis')) . '.jpg';
            $file = $dir . $filename;

            // copying
            copy($_FILES['file']['tmp_name'], $file);

            // displaying file
            $array = array('filelink' => base_url() . 'uploads/user_post_uploads/' . $filename);

            echo stripslashes(json_encode($array));

        }
    }
}

I basically created a controller function similar to the example then I reference that in the redactor function. Doesn't seem to work... I keep getting these errors in the console:

POST http://localhost/appname/user/simple_upload 500 (Internal Server Error) - /improciety/user/simple_upload:1

Uncaught TypeError: Cannot read property '0' of null - redactor.js:3100

Redactor.uploadLoaded - redactor.js:3100

g - jquery.js:2

f.event.dispatch - jquery.js:3

h.handle.i - jquery.js:3

In controller:

 function simple_upload()() {
   $config = array('upload_path' => './uploads/user_post_uploads/',
                'upload_url' => base_url()  . './uploads/user_post_uploads/',
                'allowed_types' => 'jpg|gif|png',
                'overwrite' => false,
                'max_size' => 512000,            
    );

    $this->load->library('upload', $config);

    if ($this->upload->do_upload('file')) {
        $data = $this->upload->data();
        $array = array(
            'filelink' => $config['upload_url'] . $data['file_name']
        );            
        echo stripslashes(json_encode($array));
    } else {
        echo json_encode(array('error' => $this->upload->display_errors('', '')));
    }
}

In view:

<script type="text/javascript">
$(document).ready(function(){

    $('#redactor_content').redactor({
        imageUpload: "<?php echo base_url(); ?>user/simple_upload",

        imageUploadErrorCallback: function(json)
        {
            alert(json.error);
        }          
    });
}); 
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM