1
Good,
At first I wanted to say that I don’t understand anything, but I really don’t know anything about Codeigniter. But the site is so done and asked me to add an image upload. And so I did, but badly. Here’s the code for the "form":
<?php echo form_open_multipart('admin/add_images_construcao');?>
<div class="form-group">
<div class="col-md-12" style="padding: 0px; margin: 0px;">
<label>Descrição</label>
<textarea name="texto" id="texto" class="col-md-12 "></textarea>
</div>
<div class="col-md-12" style="padding: 0px;">
<input type="file" name="userfile" onchange="readURL(this);" size="20" />
<input type="submit" value="Upload" class="btn btn-default"/>
</div>
</div>
</form>
This was quite easy. Now comes the code I’ve been working on, taking a little bit of that and there.
public function add_images_construcao() {
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$this->load->helper('url');
$this->load->database();
$id = $this->input->post('id');
$texto = $this->input->post('texto');
$nome_pasta = $id;
$dir = './assets/img/construcao/'.$nome_pasta;
if (file_exists($dir)) {
echo '';
} else {
mkdir('./assets/img/construcao/' . $nome_pasta, 0777, TRUE);
}
$config['upload_path'] = './assets/img/construcao/'.$nome_pasta;
$config['allowed_types'] = '*';
$config['max_size'] = '10000';
$this->load->library('upload', $config);
if(!$this->upload->do_upload()){
$error = array('error' =>$this->upload->display_error());
$this->load->view('admin/home', $error);
}else{
$file_data = $this->upload->data();
$data['img'] = base_url().'/assets/img/construcao/'.$file_data['file_name'];
$data = array (
"id" => $id,
"texto" => $texto,
"ficheiro" => $file_data['file_name']
);
$this->db->insert('construcao', $data);
$this->load->view('admin/header');
$this->load->view('admin/navigation', $data);
$this->load->view('admin/home', array('error'=>''));
$this->load->view('admin/footer');
}
redirect('admin/home', 'refresh');
}
else
{
redirect('home', 'refresh');
}
$this->load->helper('url');
$this->load->helper('form');
$this->load->model('user','',TRUE);
$data = array(
'titulo' => $this->input->post('titulo'),
'texto' => $this->input->post('texto'),
'data' => date('d-M-y')
);
$this->db->insert('noticias', $data);
redirect('admin/home', 'refresh');
}
And this is what I did after a month.. Someone can help me?