Welcome Guest, Not a member yet? Register   Sign In
Upload issue!
#1

[eluser]the_unforgiven[/eluser]
Here's my controller and model that uploads the image to both the uploads folder then resizes, but i need to change this to allow at least 4 uploads at once how can i do this based on my code?

Code:
[b]Controller:[/b]
function __construct()
{
      parent::__construct();
   session_start();
      $this->gallery_path = realpath(APPPATH . '/../uploads');
      $this->gallery_path_url = base_url().'uploads/';
    
  
}
function _do_upload_file()
{
  
    //upload config
  $config = array(
   'allowed_types' => 'jpg|jpeg|gif|png|pdf',
   'upload_path' => $this->gallery_path,
   'max_size' => 6000,
   'overwrite' => false,
      'remove_spaces' => true
  );
  
     $this->load->library('upload', $config);
  $this->upload->initialize($config);  
  
     if (!$this->upload->do_upload())
     {
         $this->form_validation->set_message('_do_upload_file', $this->upload->display_errors());
         return FALSE;
     }
     else
     {
  // Resize Config
   $config['image_library'] = 'gd2';
   $config['create_thumb'] = TRUE;
   $config['source_image'] = $this->upload->upload_path.$this->upload->file_name;
   $config['new_image'] = $this->gallery_path . '/thumbs';
   $config['maintain_ratio'] = TRUE;
   $config['width'] = 150;
         //echo $this->upload->upload_path.$this->upload->file_name . "<BR>";  
   $config['height'] = 100;
  
   $this->load->library('image_lib', $config);
   $this->image_lib->initialize($config);
   $this->image_lib->resize();
    
   if (!$this->image_lib->resize()){
          $this->form_validation->set_message('_do_upload_file', $this->upload->display_errors());              
   }
  
  }

}

Code:
[b]Model[/b]
function addproduct(){
  
  $data = array(  
   'name' => $this->input->post('name'),
   'short_description' => $this->input->post('short_description'),
   'long_description' => $this->input->post('long_description'),
   'status' => $this->input->post('status'),
   'category' => $this->input->post('category'),
   'featured' => $this->input->post('featured'),
   'price' => $this->input->post('price'),
   'code' => $this->input->post('code'),
   'supplier' => $this->input->post('supplier'),
   'supplier_code' => $this->input->post('supplier_code'),
   'weight' => $this->input->post('weight'),
   'stock' => $this->input->post('stock'),
   'color' => $this->input->post('color'),
   'sizes' => $this->input->post('sizes')
  
  );
  $image_data = $this->upload->data();
  $data['image'] = $image_data['file_name'];
  
  $this->db->insert('products', $data);
  redirect('admin/products');
  
  }

So based on this can someone help show/explain how i can change this code to allow 4 uploads! in to 4 separate fields in the database, which is:
Code:
id  name  short_description  long_description  image  image2  image3  image4  status
#2

[eluser]BrokenLegGuy[/eluser]
http://ellislab.com/forums/viewthread/216638/
#3

[eluser]the_unforgiven[/eluser]
Yes I couldnt get your method to work, hence posting again, plus ive changed a few things
#4

[eluser]BrokenLegGuy[/eluser]
[quote author="the_unforgiven" date="1336504143"]Yes I couldnt get your method to work, hence posting again, plus ive changed a few things[/quote]

what went wrong? It does require a change to to way that you're currently handling the file upload and retrieving errors.

The foreach would start just before
Code:
$this->load->library('upload', $config);
and close after the else statement

You could just run the _do_upload_file() function the 3 or 4 times that you need as well.
#5

[eluser]the_unforgiven[/eluser]
yes was thinking runnig the du upload again and again with a different name but be a right ball ache, i just couldnt get my head what i need to change and do.
#6

[eluser]BrokenLegGuy[/eluser]
I'll write up an explanation tonight. Basicly it give you an array like the one in the User Guide for each file. If there's an error there will be a key called display_errors (I believe, I'll have to check).

Pretty much you're looking for that key and if it's not NULL then there was a problem and that key just needs to then be displayed on screen so the user knows what they effed up.
#7

[eluser]the_unforgiven[/eluser]
Cool, would really appreciate that.
#8

[eluser]BrokenLegGuy[/eluser]
here's an example of the code in action. it's a bit crude but I think you'll be able to mess with it to fit your needs.


Code:
============= config/autoload.php =============

$autoload['libraries'] = array('upload');


============= controllers/welcome.php =============

class Welcome extends CI_Controller {
function __construct()
{
  parent::__construct();
  $this->load->helper(array('form', 'url'));
}
public function index()
{
  //this array would be best pulled from a table
  //you could also add/remove fields based off of how you want to form to function
  $file_fields = array('file_one',
        'file_two',
        'file_three',
        'file_four');
  
  if($this->input->post('submit'))
  {// I have it set up like this because it's quick and in dev it won't throw an error
   //this is the returned array
   $my_files = $this->do_upload($file_fields);
   //with a little bit of elbow grease you pull the $my_files['file_one']['file_name'] to be inserted into your database along with the other fields.
   $this->data['messages'] = $my_files;
  }
  else
  {
   $this->data['messages'] = NULL;
  }
  
  $this->load->view('welcome_message', $this->data);
}

function do_upload($fields)
{
  $result_array = NULL;
  $config['upload_path']  = 'data/';
  $config['allowed_types'] = '*';
  
  foreach($fields AS $k => $v)
  {
   $this->upload->initialize($config);
   $this->load->library('upload', $config);
  
   unset($result_array[$v]);
  
   if($this->upload->do_upload($v) === FALSE)
   {
    $result_array[$v] = $this->upload->display_errors();
   }
   else
   {
    $result_array[$v] =  $this->upload->data();
   }
  }
  return $result_array;
}
}

============= views/welcome_message.php =============

  <p>
        &lt;?php print_r($messages)?&gt;
        </p>
        &lt;?php echo form_open_multipart();?&gt;
        <p>
        &lt;input type="file" name="file_one" /&gt;&lt;br>
        <strong>
   &lt;?php
            //this was done for time sake... display errors/file names however you want
            echo ( count($messages['file_one']) > 1) ? $messages['file_one']['file_name'] : $messages['file_one'];
            ?&gt;
        </strong>
        </p>
        <p>
        &lt;input type="file" name="file_two" /&gt;&lt;br>
        <strong>
   &lt;?php
            //this was done for time sake... display errors/file names however you want
            echo ( count($messages['file_two']) > 1) ? $messages['file_two']['file_name'] : $messages['file_two'];
            ?&gt;
        </strong>
        </p>
        <p>
        &lt;input type="file" name="file_three" /&gt;&lt;br>
        <strong>
   &lt;?php
            //this was done for time sake... display errors/file names however you want
            echo ( count($messages['file_three']) > 1) ? $messages['file_three']['file_name'] : $messages['file_three'];
            ?&gt;
        </strong>
        </p>
        <p>
        &lt;input type="file" name="file_four" /&gt;&lt;br>
        <strong>
   &lt;?php
            //this was done for time sake... display errors/file names however you want
            echo ( count($messages['file_four']) > 1) ? $messages['file_four']['file_name'] : $messages['file_four'];
            ?&gt;
        </strong>
        </p>
        <br>
        &lt;input type="submit" name="submit" value="Submit" /&gt;
        &lt;?php echo form_close();?&gt;

Sorry I didn't get back sooner. I was working on the house rather late and I was tired. Smile
#9

[eluser]the_unforgiven[/eluser]
No no thats great really appreictae it and didnt expect a prompt resonse anyhow i'll give this a try.
#10

[eluser]the_unforgiven[/eluser]
Dont suppoose you have the model to go with this too do you?




Theme © iAndrew 2016 - Forum software by © MyBB