如何将编码点火器与编校者 js 图像上传 (所见即所得) 集成



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

这似乎很容易,但我无法让它工作。我的控制台中不断收到 500 错误。这是我目前的编码:

编校者 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));
        }
    }
}

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

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

在控制器中:

 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>

最新更新