多次上传调用代码点火器控制器

Uploadify calling CodeIgniter controller multiple times

本文关键字:代码 点火器 控制器 调用      更新时间:2023-09-26

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

我目前正在开发一个基于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 - 添加获取/设置/检查键 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();
}

我的上传控制器具有上述$key检查和文件上传代码。

请注意,有 2 个控制器 - 新闻和上传!

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

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

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

谢谢!

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

所以取而代之的是:

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

这样做:

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

并从admin/uploadify控制器函数添加到news/edit代码。