简体   繁体   中英

Uploadify calling CodeIgniter controller multiple times

First time here, hopefully not the last.

I'm currently developing an application based on CodeIgniter. All was well until I realised (by using CI logging) that the Uploadify script used in the page view is calling the controller multiple times.

To explain a little better...

My news controller has an edit containing this code

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

In my view I have Uploadify called like this

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

Edit - adding Uploadify controller

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

}

}

Edit No2 - adding get/set/check key 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();
}

And my uploadify controller has the $key check mentioned above and the file upload code.

Please mind that there are 2 controllers - News and Uploadify!

The problem is that by isolating certain parts of code I found out that the Uploadify Javascript is calling News controller again. Here's the link to log - http://pastebin.com/JkHAy5cN

The problem with that happening is that the key is being reset and the key passed to Javascript and the one stored in database don't match. I'm stumped on this one.

Any suggestions, fixes, comments are more than welcome.

Thank you!

You would not use 2 controller functions simultaneously, just one. That's not how CI works. So point your javascript to the news/edit controller function. In this function, you need to then include the code that does the actual upload as i'm not seeing it above. But first things first, correct your JS.

so instead of this:

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

do this:

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

and add to news/edit your code from the admin/uploadify controller function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM