如何将codeigniter与redactor js image upload(wysiwyg)集成?

时间:2012-11-14 02:29:51

标签: php ajax codeigniter upload wysiwyg

我正在尝试将Codeigniter与名为redactor js的所见即所得编辑器一起使用。 基本上,我想要完成的任务可以在这里找到:http://imperavi.com/redactor/docs/images/

看起来很容易,但我无法让它发挥作用。我的控制台一直出现500错误。这是我目前的编码:

Redactor js:

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

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

处理上传的PHP类:

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));

        }
    }
}

我基本上创建了一个类似于示例的控制器函数,然后我在redactor函数中引用它。似乎不起作用......我一直在控制台中收到这些错误:

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

1 个答案:

答案 0 :(得分:3)

在控制器中:

 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('', '')));
    }
}

在视图中:

<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>