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

how to fix unwanted double copy of uploaded file?

我的代码 运行 没问题,可以正确添加文件,但它会添加一份已上传文件的额外副本。怎么了?

我的控制器是这样的:

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

... but it is adding one additional copy of uploaded file. What is wrong?

那是因为你调用了 do_upload() 两次...

public function add_audio() 
{
    ....

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

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

由于您可能需要处理上传失败的条件逻辑,因此请删除 do_upload().

first 实例