Welcome Guest, Not a member yet? Register   Sign In
Storing image filename to DB along with other data
#1

[eluser]RockyS[/eluser]
Hello,

I have a form with multiple inputs (text) and one file input(for images) what I would like to do is to store image filename to DB with other submitted data,
this is my controller http://pastebin.com/K6s34xRe and my
model http://pastebin.com/9ddj4uTX
(method "dodaj_sliko() is save_image() in english)

My field for files is
Code:
<input type="file" name="file_upload_manual_slike" multiple />

So the main question is, how can I accomplish this, because above code that I posted doesn't work :-S
#2

[eluser]Oscar Dias[/eluser]
Hi,
I think that your upload is not executed because it is right after a redirect:
Code:
redirect($this->config->item('base_url'), 'refresh');
$this->do_upload($this->db->insert_id());

The redirect function has an exit, which means it will stop execution after it.
Code:
function redirect($uri = '', $method = 'location', $http_response_code = 302)
{
  if ( ! preg_match('#^https?://#i', $uri))
  {
   $uri = site_url($uri);
  }

  switch($method)
  {
   case 'refresh' : header("Refresh:0;url=".$uri);
    break;
   default   : header("Location: ".$uri, TRUE, $http_response_code);
    break;
  }
  exit;
}

You need to do the upload before the redirect.
#3

[eluser]ppwalks[/eluser]
are you using codeigniters upload class if so you pass the variable to the model like this, here is mine using "do_upload":
Model:
Code:
function add_product($file_name) {
  $data = array(
  'product_name' =>$_POST['product_name'],
  'description' =>$_POST['product_description'],
  'price' => $_POST['product_price'],
  'discount_price' => $_POST['discount_price'],
  'thumbnail'=> $file_name,
  'image'=> $file_name
  );
   $this->db->insert('product', $data);
}
And here is the portion of my controller
Code:
if (!empty($_FILES['product_thumb']))
        {
            // Specify configuration for File 1
            $config['upload_path'] = './shop_images/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '100';
            $config['max_width']  = '1024';
            $config['max_height']  = '768';      

            // Initialize config for File 1
            $this->upload->initialize($config);

            // Upload file 1, product_thumb is the name of my input for upload
            if ($this->upload->do_upload('product_thumb'))
            {
                $data = array(
                    'thumbnail' => $this->upload->data()
                );
     $file_name = $data['thumbnail']['file_name'];
    
            }
            else
            {
                echo $this->upload->display_errors();
            }
then pass it through to the model like so:
Code:
$this->MProducts->add_product($file_name);




Theme © iAndrew 2016 - Forum software by © MyBB