0

第一次来这里,希望不是最后一次。

我目前正在开发基于 CodeIgniter 的应用程序。一切都很好,直到我意识到(通过使用 CI 日志记录)页面视图中使用的 Uploadify 脚本多次调用控制器。

为了更好地解释......

我的新闻控制器有一个包含此代码的编辑

public function edit(){
    $id = (int)$this->uri->segment(4);

    if (empty($id)){
        $this->session->set_flashdata('error', "ID can't be empty");
        redirect('admin/news');
    }

    //article data called here and stored in $data
    $data = $this->news_model->fetch_article($id);

    $data['title'] = "News";
    $data['news_links'] = $this->news_model->get_news_links();
    $data['form_url'] = "admin/edit";
    $data['available_pages'] = $this->news_model->get_article_pages();


    $data['css'] = "/css/extra/uploadify.css";
    $data['js'] = "/js/extra/jquery.uploadify-3.1.min.js";
    $data['edit'] = true;


    //set uploadify key
    $data['key'] = md5(time() . $this->session->userdata('session_id'));
    // a unique key is created, then stored in database
    // the key is then passed to the uploadify script
    // when the upload is being done it is checked with the one in the database
    // if the two match files are uploaded. This is a workaround for flash
    // session problem
    $this->news_model->set_key($data['key']);

    if($this->input->post('submit')){
        $this->save_article("update",$id);
    }


    $this->load->view("admin/news",$data);
}

在我看来,我已经像这样调用 Uploadify

    $('#extra_data').uploadify({
    'swf'      : '<?php echo site_url(); ?>flash/uploadify.swf',
    'uploader' : '<?php echo site_url(); ?>admin/uploadify',
    'multi'     : true,
    'auto'      : false,
    'fileTypeExts'     : '*.jpg;*.jpeg;*.gif;*.png',
    'fileTypeDesc'    : 'Images',
    'formData'      : {'key' : '<?php echo $key; ?>'},
    'onQueueComplete': function(){
        ajax_images(); //calls a function that loads uploaded image thumbnails
    }
}); 

编辑 - 添加 Uploadify 控制器

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Uploadify extends CI_Controller{

function __construct(){
    parent::__construct();

    // contains set_key / check_key / get_key / add_image functions
    $this->load->model('admin/news_model');
}

public function index(){
    if($this->news_model->check_key($this->input->post("key",true))){

        $this->upload_image();

    }else{
        log_message('error', "The hash key isn't correct! No upload has been done!");
    }
}

public function load_images(){
    $data = $this->news_model->get_images($this->uri->segment(4));

    foreach($data as $image){
        ?>
        <div style="background:url(<?php echo site_url(); ?>images/uploads/thumb_<?php echo $image->file; ?>) no-repeat"> 
            <a class="delete" href="javascript:void(0);" onclick="remove_pic('<?php echo site_url(); ?>admin/news/remove_picture/<?php echo $image->id; ?>')"></a>
        </div>
        <?php
    }
}

private function upload_image($case = "article"){
    $this->load->library("imaging");
    $this->imaging->upload($_FILES['Filedata']);

    $file_name = md5(time().$this->input->post("key"));
    $target_path = $_SERVER['DOCUMENT_ROOT'] . "/images/uploads/";

    if ($this->imaging->uploaded){

        $this->imaging->file_new_name_body   = $file_name;
        $this->imaging->file_auto_rename      = true;
        $this->imaging->image_resize         = false;
        $this->imaging->mime_check          = true;
        $this->imaging->allowed = array('image/*');
        $this->imaging->process($target_path);

        $this->imaging->file_name_body_pre = 'thumb_';
        $this->imaging->file_new_name_body   = $file_name;
        $this->imaging->image_resize          = true;
        $this->imaging->image_ratio_crop      = true;
        $this->imaging->image_y               = 148;
        $this->imaging->image_x               = 148;
        $this->imaging->process($target_path);

        if ($this->imaging->processed) {
            $this->news_model->add_image($this->input->post("key",true),$file_name . "." . $this->imaging->file_src_name_ext);
            $handle->clean();
        } else {
            log_message('error', $this->imaging->error);
        }
    }else{
        log_message('error', $this->imaging->error);
    }

}

}

编辑 No2 - 添加 get/set/check 键 f-ions

function set_key($key)
{

    $data['key'] = $key;

    $this->db->where('id', 1);
    $this->db->update('uploadify_key', $data);

    log_message("debug", "Uploadify key set to " . $key);
}



function get_key($id)
{

    $this->db->select('key');
    $this->db->where('id', $id);
    $query = $this->db->get('uploadify_key');

    $row = $query->row();

    return $row->key;


}

function check_key($key,$id = 1)
{

    $db_key = $this->get_key($id);

    if($key == $db_key)
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }

}

function clear_key($id){
    $data['key'] = "";

    $this->db->where('id', $id);
    $this->db->update('uploadify_key', $data);
}

function add_image($hash, $file){
    $data['file'] = $file;
    $data['hash'] = $hash;

    $this->db->insert("images", $data);
}

function get_images($hash){
    $this->db->select("id, file");
    $return = $this->db->get_where("images",array("hash" => $hash));

    return $return->result();
}

我的uploadify控制器有$key上面提到的检查和文件上传代码。

请注意,有 2 个控制器 - News 和 Uploadify!

问题是通过隔离代码的某些部分,我发现 Uploadify Javascript 再次调用 News 控制器。这是日志的链接 - http://pastebin.com/JkHAy5cN

发生这种情况的问题是密钥正在被重置,并且传递给 Javascript 的密钥与存储在数据库中的密钥不匹配。我被这个难住了。

任何建议、修复、评论都非常受欢迎。

谢谢!

4

1 回答 1

0

您不会同时使用 2 个控制器功能,只能使用一个。CI 不是这样工作的。因此,将您的 javascript 指向news/edit控制器功能。在此函数中,您需要包含执行实际上传的代码,因为我在上面没有看到它。但首先,纠正你的 JS。

所以而不是这个:

    'uploader' : '<?php echo site_url(); ?>admin/uploadify',

做这个:

    'uploader' : '<?php echo site_url(); ?>news/edit',

从控制器功能添加到news/edit您的代码中。admin/uploadify

于 2012-05-07T22:29:31.330 回答