Welcome Guest, Not a member yet? Register   Sign In
Uploading a file in a form with more data

I'm building a website where I want to upload a product with it's name price and description to the database, and upload an image of the product to a folder. I need the name of the image in the database too.
I've tried to change the name of the image, changing the permissions of the images folder, etc.
When I add a new product it is created in the database but the image name field is empty and the image is not uploaded.
When I print the array with $this->upload->data() it is empty.
Here's my code:
Function submit_product 
public function submit_product(){
       $config['upload_path'] = base_url('images/');
       $config['allowed_types'] = 'jpg|png';
       $config['max_size'] = '2048';
       $config['max_width'] = '2048';
       $config['max_height'] = '2048';
       $this->load->library('upload', $config);
       $this->form_validation->set_rules('name', 'Name', 'required|trim');
       $this->form_validation->set_rules('price', 'Price', 'required|trim');
       $this->form_validation->set_rules('category', 'Category', 'required|trim');
       $this->form_validation->set_rules('description', 'Description', 'trim');
           $filename = $this->upload->data();
       } else {
Here is the form
               echo form_open_multipart('main/submit_product');
               echo "<p>Name: ";
               echo form_input('name',$this->input->post('name'));
               echo "</p>";
               echo "<p>Price: ";
               echo form_input('price', $this->input->post('price'));
               echo "</p>";
               echo "<p>Category: ";
               echo form_input('category', $this->input->post('category'));
               echo "</p>";
               echo "<p>Description: ";
               echo form_input('description', $this->input->post('description'));
               echo "</p>";
               echo "<p>";
               echo form_upload('userfile');
               echo "</p>";
               echo "<p>";
               echo form_submit('product_submit','Add Product');
               echo "</p>";
               echo "<p>";
               echo form_close();
               echo "</p>";
And here is the model
public function add_product($filename){
       print_r ($filename);
       $data = array (
           'name' => $this->input->post('name'),
           'price' => $this->input->post('price'),
           'image' => $filename['file_name'],
           'category' => $this->input->post('category'),
           'description' => $this->input->post('description')

suggest that you try doing just the upload first - then add the rest of the form
this page has sample code for uploading images http://www.codeigniter.com/user_guide/li...ading.html

Theme © iAndrew 2016 - Forum software by © MyBB