Welcome Guest, Not a member yet? Register   Sign In
Form with image upload
#1

[eluser]zyklon[/eluser]
I am trying to make a form with 2 text fields and a file field.I am trying to validate it using validator class.I don't know how could i verify that the image should only be jpeg,png,gif.The form is working for the text fields but i don't know how to make the validaton for file field type.I tried to use a callback function but it doesn't work.Basically i need a solution to post data from a form with text fields but also file fields.
Actually i tried 2 ways:
1.With a callback function with the content:
function check_photo_extension()
{

$config['upload_path'] = "./upload/";
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
this->load->library('upload', $config);
$upload_files = $this->upload->data();
$ext = $upload_files["file_ext"];
if($ext != ".jpg" || $ext != ".JPG" || $ext != ".gif" || $ext != ".GIF" || $ext != ".png" || $ext != ".PNG")
{
$this->validation->set_message('check_photo_extension',"Image must be a gif,jpg or png.");
}
}

2.With a function that also make the upload of the photo:
function upload_photos()
{
$view_data["action"] = "add_sponsors";

$this->load->helper(array('form', 'url'));

$config['upload_path'] = "./upload/";
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
this->load->library('upload', $config);



if ( ! $this->upload->do_upload("userfile"))
{
$error = $this->upload->display_errors();
$view_data["error"] = "Error";

$this->validation->set_message('upload_photos', "$error");

return false;

}
else
{

$image_lib_config['image_library'] = 'gd2';
$image_lib_config['maintain_ratio'] = TRUE;
$image_lib_config['quality'] = '70';

$upload_file = $this->upload->data();
$file_name = $upload_file["file_name"];
$image_width = $upload_file['image_width'];
$image_height = $upload_file['image_height'];


$image_lib_config['master_dim'] = 'auto';
$image_lib_config['width'] = '300';
$image_lib_config['height'] = '200';
$image_lib_config['source_image'] = './upload/'.$file_name;
$image_lib_config['create_thumb'] = FALSE;

$this->image_lib->initialize($image_lib_config);

if(!$this->image_lib->resize())
{
$error = $this->image_lib->display_errors();
$this->validation->set_message('upload_photos',"$error");

}
else
{
return $post_image = $file_name;
}
}
}


And the form is:

public function add_sponsors()
{
$view_data["action"] = "add_sponsors";
$this->load->helper(array('form', 'url'));

$this->load->model('Admin_model','admin');
$view_data['internal_error']="";
$this->load->library('validation');

$rules["name"] = "trim|required";
$rules["description"] = "trim|required";
$rules["website"] = "trim";
$rules["userfile"] = "callback_check_photo_extension";
$this->validation->set_rules($rules);
$fields["name"] = "Nume";
$fields["description"] = "Descriere";
$fields["website"] = "Website(without http://)";
$fields["userfile"] = "Logo";
$this->validation->set_fields($fields);
$this->validation->set_message("required","Nu ati completat campul %s");
$this->validation->set_error_delimiters('<br>', '');
if($this->validation->run() == FALSE)
{
$this->load->view('admin/front/admin_front_view', $view_data);
}

else
{
$result_sponsors = $this->admin->check_sponsors("sponsors",$this->input->post('name','TRUE'));
if($result_sponsors == TRUE)
{
$name = $this->input->post('name','TRUE');
$description = $this->input->post('description','TRUE');
$website = $this->input->post('website','TRUE');
$image = $this->input->post("userfile");
$this->load->library('upload');
$this->load->library('image_lib');
$post_image = $this->upload_photos();
$result_create_sponsors = $this->admin->create_sponsors("sponsors",$name,$description,$website,$post_image);
if($result_create_sponsors == TRUE)
{
header("Location: ". $this->config->item('base_url')."index.php/admin/view_sponsors");
}
else
{
$view_data['internal_error'] = 'Inregistrarea nu a putut fi facuta';
$this->load->view('admin/front/admin_front_view', $view_data);
}
}
else
{
$view_data['internal_error']='Acest sponsor deja exista';
$this->load->view('admin/front/admin_front_view', $view_data);
}
}




}




Theme © iAndrew 2016 - Forum software by © MyBB