[eluser]abmcr[/eluser]
A rapyd copy/paste from my code....Send me if you don't understand
Ciao
Code:
function testi_edit()
{
//librerie
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->load->library('upload');
//model for loading data from db
$this->load->model("welcome_model","modello");
//validatori
$this->form_validation->set_rules('txtitaliano', 'Testo italiano', 'required');
//this line for enablin the requeired file input
//$this->form_validation->set_rules('prova_file', 'prova_file', 'callback_file_check');
//recupera i dati del record
$cid = (int) $this->input->post('id');
$data['id'] = ($cid) ? $cid : $this->uri->segment($this->uri->total_segments());
$data['row'] = $this->modello->get_data($data['id'],"testi");
//config for a file
$config_prova_file['upload_path'] = './uploads/';
$config_prova_file['allowed_types'] = 'gif|jpg|png|pdf';
$config_prova_file['max_size'] = '1000';
$config_prova_file['max_width'] = '1024';
$config_prova_file['max_height'] = '768';
$config_prova_file['thumb'] = '100|100';
//imposta array per messaggi errore
$error=array();
//upload the files
foreach($_FILES as $key=>$value )
{
if ( ! $_FILES[$key]['size']==0){
//carica il config del caso
$config="config_$key";
$this->upload->initialize($$config);
//go with the update
if ( ! $this->upload->do_upload($key))
{
$error[$key]= $this->upload->display_errors();
}else{
$data_file=($this->upload->data());
$this->db->set($key,$data_file['file_name']);
}
}
}
//avvia o il form o la scrittura sul db
if (($this->form_validation->run() == FALSE)||(count($error)>0)){
$data['error']=$error;
$data["content"]=$this->load->view("form",$data,TRUE);
//vista finale
$this->load->view('admin/tema',$data);
}
else
{
//inserisce nel db
$this->db->set('rel_categoria',$this->input->post("rel_categoria"));
$this->db->set('id_padre',$this->input->post("id_padre"));
$this->db->set('livello',$this->input->post("livello"));
$this->db->set('menu_pagine_aggiunte',$this->input->post("menu_pagine_aggiunte"));
$this->db->set('master',$this->input->post("master",0));
$this->db->set('pagine',$this->input->post("pagine",0));
$this->db->set('data',$this->input->post("data","00-00-0000 00:00:00"));
$this->db->where('id', $data['id']);
$this->db->update('testi');
//flash message
$this->session->set_flashdata('msg_edit', 'I dati sono stati salvati con successo');
$this->session->set_flashdata('color_msg_edit', 'green');
redirect("ad/testi");
}
}
/**
* callback per file required
*/
function file_check($str)
{
$flag=TRUE;
if (($str == '')&&(($_FILES['foto']['size'])==0))
{
$this->form_validation->set_message('file_check', 'The %s field can not be empty');
$flag=FALSE;
}
return $flag;
}
IN the view
Code:
//this function is in an extended helper
function form_error_upload($chiave,$errori){
return(array_key_exists($chiave,$errori)?$errori[$chiave]:NULL);
}
...
echo br();
echo form_error_upload("prova_file",$error);echo "\n";
echo form_hidden("prova_file",$row->prova_file);echo "\n";
echo form_label('Allegato', 'prova_file');echo "\n";
echo form_upload("prova_file",array("size"=>30));echo "\n";