如何修复上传文件的不需要的双重副本?

时间:2016-07-27 14:45:01

标签: php codeigniter file-upload codeigniter-3

我的代码运行正常并正确添加文件,但它正在添加一个上传文件的附加副本。有什么问题?

我的控制器是这样的:

public function add_audio(){

            $config['upload_path'] = './musics/';
            $config['allowed_types'] = 'mp3';
            $config['max_size'] = '999999999999999999999';

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



    if ( ! $this->upload->do_upload())
        {
            $data['error'] = $this->upload->display_errors();
            print_r($data['error']);    
            //line of codes that displays if there are errors
        }
    else
    {
        $data['audio'] = $_FILES['userfile']['name'];

        $this->load->model('main');
        $query = $this->main->insert('audio',$data);

        if($query == TRUE){
            $this->load->view('admin/success');
        }
    }

1 个答案:

答案 0 :(得分:0)

  

...但它正在添加一个上传文件的附加副本。有什么问题?

那是因为你两次打电话给do_upload() ......

public function add_audio() 
{
    ....

    $this->upload->do_upload(); // <- FIRST upload (remove this one)

    if ( ! $this->upload->do_upload() ) // <- SECOND upload
    {
        ....

由于您可能需要处理上传失败的条件逻辑,因此请删除do_upload()第一个实例。