php – Error uploading videos on CodeIgniter

Question:

I'm not able to upload videos on CodeIgniter, the function works with pdf but not with other extensions.
The feedback I get is file type not allowed .

Controller:

public function add(){
    $dados['tutorial']= $this->uploadArquivo('mp4', 'tutorial');
    $dados['video']= $this->uploadArquivo('mp4', 'video');
    $dados['apostila']= $this->uploadArquivo('pdf', 'apostila');
    $ok= $this->crud->add('roteiro', $dados);
    if ($ok){                           
        echo "<script>alert('Roteiro adicionado com sucesso');</script>";
        $this->load->view('includes/header');           
        $this->load->view('principal');
        $this->load->view('includes/footer');
    }
    else{
        echo "<script>alert('Cadastro não efetuado');</script>";
        $this->load->view('includes/header');           
        $this->$this->load->view('principal');          
        $this->load->view('includes/footer');
    }
}

public function uploadArquivo($tipo= NULL, $nome=NULL){
    $config['upload_path'] = 'assets/arquivos/';
    $config['allowed_types'] = $tipo;
    $config['max_size']  = '0';
    $config['max_width']  = '0';
    $config['max_height']  = '0';
    $config['encrypt_name']  = 'true';

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

    if ( !$this->upload->do_upload($nome)){
        $error = array('error' => $this->upload->display_errors());
        echo $nome. " ".$error['error'];
        exit();
    }
    else{
        $arquivo= $this->upload->data();
        return $arquivo['file_name'];
    }
}

View:

$attributes= array('id'=>'frmCadastro', 'name'=>'frmCadastro'); echo form_open_multipart('script/add', $attributes); echo form_fieldset('Register Script'); $attributes='';

        $atributos= array('name'=>'lblTutorial', 'id'=>'lblTutorial', 'class'=>'control-label');
        echo form_label('Upload Tutorial', '', $atributos);

        $atributos= array('id'=>'tutorial', 'name'=>'tutorial');
        echo form_upload($atributos);
        $atributos= '';     

        $atributos= array('name'=>'lblVideo', 'id'=>'lblVideo');
        echo form_label('Upload do Vídeo', '', $atributos);
        $atributos= ''; 

        $atributos= array('id'=>'video', 'name'=>'video');
        echo form_upload($atributos);

        $atributos= array('name'=>'lblApostila', 'id'=>'lblApostila');
        echo form_label('Upload da Apostila', '', $atributos);
        $atributos= ''; 

        $atributos= array('id'=>'apostila', 'name'=>'apostila');
        echo form_upload($atributos);
        $atributos= '';                 

        $atributos= array('id'=>'cadastrar', 'name'=>'cadastrar'); 
        echo form_submit($atributos, 'Cadastrar');
        $atributos= '';                     
        echo form_fieldset_close();
        echo form_close();

Answer:

I don't know which version of your CodeIgniter you are using, but check the mime-type of the file you are uploading. If you are using version 2.3 you do not have mp4 in the list of valid mimes as you can see in the ´./application/config/mimes.php´ file. What I advise is to take a look at the latest version on github https://github.com/EllisLab/CodeIgniter/blob/develop/application/config/mimes.php which already has validation for this type of file.

Just remembering to check the file size, in the php.ini settings how much it supports, but this I imagine you've already seen.

Scroll to Top