如何将codeigniter与redactor js图像上传(wysiwyg)集成在一起?

时间:2021-12-06 02:02:25

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/

我尝试使用Codeigniter和wysiwyg编辑器redactor js。基本上,我想要完成的任务可以在这里找到: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:

这看起来很容易,但我做不到。我的控制台总是出错500次。这是我目前的编码:

Redactor js:

编辑者js:

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

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

PHP class that handles the uploads:

处理上传的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));

        }
    }
}

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:

我基本上创建了一个类似于这个例子的控制器函数,然后我在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 个解决方案

#1


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>

#1


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>