简体   繁体   English

如何在codeigniter的同一控制器类中多次调用控制器方法?

[英]How to call a controller method multiple times in the same controller class in codeigniter?

I am new to codeigniter, I have a problem with calling a method multiple times in the same class, here are my two controller methods. 我是Codeigniter的新手,在同一个类中多次调用方法时遇到问题,这是我的两个控制器方法。

Clients.php Clients.php

public function upload_file(){
    $Cover_path = './resources/Cover/';
    $Preach_path = './resources/Preaches/';
    $Speach_path = './resources/Speaches/';
    $Song_path = './resources/Songs/';
    $Writting_path = './resources/Writtings/';

    $C_allowed_types ='jpg|png|jpeg';
    $allowed_audio_types ='*';
    $allowed_writtings_types ='pdf|docx|doc';

    $C_max_size = '2500000';
    $W_max_size = '5000000';
    $A_max_size = '6000000';

    $width = '0';
    $height = '0';
    $width1 = '256';
    $height1 = '256';
    $width2 = '128';
    $height2 = '128';

    $C_fileName = 'cover_page';
    $Other_fileName = 'file';

    $action = 'user_uploads';

    $file_type = $this->input->post('activity');

    if($file_type == 'Writtings'){
        $cover_path = $this->do_upload($action,$Cover_path,$C_allowed_types,$C_max_size,$width,$height,$C_fileName);
        $file_path = $this->do_upload($action,$Writting_path,$allowed_writtings_types,$W_max_size,$width,$height,$Other_fileName);
    }elseif($file_type == 'Song'){
        $cover_path = $this->do_upload($action,$Cover_path,$C_allowed_types,$C_max_size,$width1,$height1,$C_fileName);
        $file_path = $this->do_upload($action,$Song_path,$allowed_audio_types,$A_max_size,$width,$height,$Other_fileName);
    }elseif($file_type == 'Preach'){
        $cover_path = $this->do_upload($action,$Cover_path,$C_allowed_types,$C_max_size,$width,$height,$C_fileName);
        $file_path = $this->do_upload($action,$Preach_path,$allowed_audio_types,$A_max_size,$width,$height,$Other_fileName);
    }elseif($file_type == 'Speach'){
        $cover_path = $this->do_upload($action,$Cover_path,$C_allowed_types,$C_max_size,$width2,$height2,$C_fileName);
        $file_path = $this->do_upload($action,$Speach_path,$allowed_audio_types,$A_max_size,$width,$height,$Other_fileName);
    }


    $upload_info = array(
        'user_email' => $this->session->userdata['email'],
        'file_name' =>$this->input->post('file_name'),
        'cover_direc' =>$cover_path,
        'file_direc' =>$file_path,
        'file_type'=>$file_type,
        'date_released' =>$this->input->post('date_released')
    );

    //var_dump($upload_info['file_direc']); die();

    $this->files->upload_file($upload_info);
    $this->session->set_flashdata('success_msg', 'Data uploaded successful.');
    //$this->session->set_userdata('ext',$upload_info);
    redirect('Clients/user_panel');

}

public function do_upload($action,$path,$allowed_types,$max_size,$width,$height,$fileName)
{
        $config['upload_path']          = $path;
        $config['allowed_types']        = $allowed_types;
        $config['max_size']             = $max_size;
        $config['max_width']            = $width;
        $config['max_height']           = $height;

        //redirect('Clients/user_panel');
        //var_dump($config['upload_path']); die();

        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload($fileName))
        {
            $data = array('error' => $this->upload->display_errors());

            $this->session->set_flashdata('error', $data);
            //redirect('Clients/user_panel');
            if($action == 'register'){
                redirect('Clients/register_view');
            }elseif($action == 'user_uploads'){
                redirect('Clients/user_panel');
            }

        }
        else
        {
            $data = $this->upload->data();

            return $data['full_path'];
        }
}

The problem is do_upload() returns the same value when called in upload_file() depending on the first call, also does not allow specifying the file type on $allowed_audio_types ='*' that's why I have used '*' , Any help please, I have googled a lot with no success. 问题是do_upload()在第一次调用中在upload_file() do_upload()返回时返回相同的值,也不允许在$allowed_audio_types ='*'上指定文件类型,这就是为什么我使用'*' ,请提供任何帮助,我用谷歌搜索了很多但没有成功。

Try using 尝试使用

$this->upload->initialize($config);

after

$this->load->library('upload', $config);

so it your code will look like 所以你的代码看起来像

$this->load->library('upload', $config);
$this->upload->initialize($config);

This will make sure that you reset the old preferences. 这将确保您重置旧的首选项。 Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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