Upload issue! - El Forum - 05-08-2012
[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
Upload issue! - El Forum - 05-08-2012
[eluser]BrokenLegGuy[/eluser]
http://ellislab.com/forums/viewthread/216638/
Upload issue! - El Forum - 05-08-2012
[eluser]the_unforgiven[/eluser]
Yes I couldnt get your method to work, hence posting again, plus ive changed a few things
Upload issue! - El Forum - 05-08-2012
[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.
Upload issue! - El Forum - 05-08-2012
[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.
Upload issue! - El Forum - 05-08-2012
[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.
Upload issue! - El Forum - 05-08-2012
[eluser]the_unforgiven[/eluser]
Cool, would really appreciate that.
Upload issue! - El Forum - 05-09-2012
[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>
<?php print_r($messages)?>
</p>
<?php echo form_open_multipart();?>
<p>
<input type="file" name="file_one" /><br>
<strong>
<?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'];
?>
</strong>
</p>
<p>
<input type="file" name="file_two" /><br>
<strong>
<?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'];
?>
</strong>
</p>
<p>
<input type="file" name="file_three" /><br>
<strong>
<?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'];
?>
</strong>
</p>
<p>
<input type="file" name="file_four" /><br>
<strong>
<?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'];
?>
</strong>
</p>
<br>
<input type="submit" name="submit" value="Submit" />
<?php echo form_close();?>
Sorry I didn't get back sooner. I was working on the house rather late and I was tired.
Upload issue! - El Forum - 05-09-2012
[eluser]the_unforgiven[/eluser]
No no thats great really appreictae it and didnt expect a prompt resonse anyhow i'll give this a try.
Upload issue! - El Forum - 05-09-2012
[eluser]the_unforgiven[/eluser]
Dont suppoose you have the model to go with this too do you?
|