上传多次调用CodeIgniter控制器

时间:2012-05-07 18:02:57

标签: php javascript codeigniter uploadify

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

我目前正在开发基于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);
}

在我看来,我上传了像这样的上传

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

修改 - 添加上传控制器

<?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个控制器 - 新闻和上传!

问题在于,通过隔离代码的某些部分,我发现Uploadify Javascript再次调用了新闻控制器。这是登录的链接 - http://pastebin.com/JkHAy5cN

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

任何建议,修正和评论都非常受欢迎。

谢谢!

1 个答案:

答案 0 :(得分: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您的代码。