I'm developing a form using CodeIgniter. In this, one of the fields is to upload a file. I followed some tutorials about file uploading in CodeIgniter successfully, but, in my work, the $_FILES is always empty, and I can't see why. I'm a newbie in this framework.
The view:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
$this->load->helper('form');
echo form_open_multipart($action); ?>
Área:
<select>
<option value="1">Teste 1</option>
<option value="2">Teste 2</option>
</select> <br>
Data de abertura: <input type="text" id="data_abertura" placeholder="tqv"> <br>
Prioridade:<br>
<input type="radio" name="prioridade" value="3">Alta<br>
<input type="radio" name="prioridade" value="2">Média<br>
<input type="radio" name="prioridade" value="1">Baixa<br>
Anexar arquivo: <input type="file" accept="image/*, text/*, .doc, .docx, .pdf" name="arquivo" id="arquivo"><br>
Descrição: <br><textarea rows="4" cols="50" id="descricao" name="descricao"></textarea><br>
<input type="submit" id="enviar" name="enviar" value="Enviar">
<?php echo form_close() ?>
</body>
</html>
The controller:
<?php class c_Formulario extends CI_Controller { public function __construct() { parent::__construct(); $this->load->helper('url'); } public function index() { $data['action'] = site_url('c_formulario/inserir_dados'); $this->load->view('v_formulario', $data); } public function inserir_dados() { $config['upload_path'] = './upload/'; $config['allowed_types'] = 'gif|jpg|png|doc|docx|pdf|txt'; $config['max_size'] = '100'; $this->load->library('upload'); $this->upload->initialize($config); var_dump($_FILES); foreach($_FILES as $field => $file) { //var_dump($_FILES); die(); // No problems with the file if($file['error'] == 0) { // So lets upload if ($this->upload->do_upload($field)) { $data = $this->upload->data(); echo $data['full_path']; } else { $errors = $this->upload->display_errors(); var_dump($errors); } } } $this->load->model('m_formulario', '', TRUE); $area = array('id' => null, 'nome' => $this->input->post('area', TRUE)); $data = array('id' => null, 'dataabert' => $this->input->post('data_abertura', TRUE)); $prioridade = array('id' => null, 'nome_prioridade' => $this->input->post('prioridade', TRUE)); $arquivo = array('id' => null, 'urlarquivo' => $this->input->post('arquivo', TRUE)); $descricao = array('id' => null, 'descricao' => $this->input->post('descricao', TRUE)); $dados = array ( 'area' => $area, 'data' => $data, 'prioridade' => $prioridade, 'arquivo' => $arquivo, 'descricao' => $descricao ); $this->m_formulario->inserir($dados); }
Thanks any help!
I don't know exactly why, but changing the allowed types I solved the issue. $config['allowed_types'] = '*'; I was testing with some txt and png files. Well, it's ok now. Sorry the bad question.
Do you have the correct configuration of all the extensions mime types (pdf,txt,...) in your config/mimes.php file?
If a mime type is missing, you can find the correct ones on google pretty easy ;)