I am building a site that let user sell their cars. a user fills in the form with necessary fields with one of the required fields being uploading images of that specific car.everything works fine with my code expect the fact that when a user upload multiple images i will have two columns with similar values expect for the uploaded images and the carID which makes it hard for me to retrieve the images.if anyone knows the better way for me to store these images correctly and retrieving them i would appreciate. Here is my database structure:
CarID | want_to | make | model |year | kms | transmission | location |price |mobile | image
and here is my controller:
// Upload new car
function add()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('list_options', 'Options', 'trim|required|min_length[2]|xss_clean');
$this->form_validation->set_rules('make', 'Make', 'trim|required|min_length[2]|xss_clean');
$this->form_validation->set_rules('model', 'Model', 'trim|required|min_length[2]|xss_clean');
$this->form_validation->set_rules('year', 'Year', 'trim|required|min_length[2]|xss_clean');
$this->form_validation->set_rules('kms', 'kms', 'trim|required|min_length[2]|xss_clean');
$this->form_validation->set_rules('transmission', 'Transmissions', 'trim|required|min_length[2]|xss_clean');
$this->form_validation->set_rules('location', 'Location', 'trim|required|min_length[2]|xss_clean');
$this->form_validation->set_rules('price', 'Price', 'trim|required|min_length[2]|xss_clean');
$this->form_validation->set_rules('mobile', 'Mobile', 'trim|required|min_length[2]|xss_clean');
$upload_conf = array(
'upload_path' => realpath('images/'),
'allowed_types' => 'gif|jpg|png',
'max_size' => '30000',
);
$this->upload->initialize( $upload_conf );
// Change $_FILES to new vars and loop them
foreach($_FILES['userfile'] as $key=>$val)
{
$i = 1;
foreach($val as $v)
{
$field_name = "file_".$i;
$_FILES[$field_name][$key] = $v;
$i++;
}
}
// Unset the useless one ;)
unset($_FILES['userfile']);
// Put each errors and upload data to an array
$error = array();
$success = array();
// main action to upload each file
foreach($_FILES as $field_name => $file)
{
if ( ! $this->upload->do_upload($field_name))
{
// if upload fail, grab error
$error['upload'][] = $this->upload->display_errors();
}
else
{
// otherwise, put the upload datas here.
// if you want to use database, put insert query in this loop
$upload_data = $this->upload->data();
$data=array(
'want_to'=>$this->input->post('list_options'),
'make'=>$this->input->post('make'),
'model'=>$this->input->post('model'),
'year'=>$this->input->post('year'),
'kms'=>$this->input->post('kms'),
'transmission'=>$this->input->post('transmission'),
'location'=>$this->input->post('location'),
'price'=>str_replace(",", "",$this->input->post('price')),
'mobile'=>$this->input->post('mobile'),
'image'=>$upload_data['orig_name'],
);
$imagedata=array(
'imagePath'=>$upload_data['orig_name'],
'imageName'=>$upload_data['file_name'],
);
$this->car_model->add_car($data);
// set the resize config
$resize_conf = array(
// it's something like "/full/path/to/the/image.jpg" maybe
'source_image' => $upload_data['full_path'],
// and it's "/full/path/to/the/" + "thumb_" + "image.jpg
// or you can use 'create_thumbs' => true option instead
'new_image' => 'images/thumbs/',
'width' => 200,
'height' => 200
);
$medium_conf = array(
// it's something like "/full/path/to/the/image.jpg" maybe
'source_image' => $upload_data['full_path'],
// and it's "/full/path/to/the/" + "thumb_" + "image.jpg
// or you can use 'create_thumbs' => true option instead
'new_image' => 'images/medium/',
'width' => 600,
'height' => 600
);
// initializing
$this->image_lib->initialize($resize_conf);
//$this->image_lib->initialize($medium_conf);
// do it!
if ( ! $this->image_lib->resize())
{
// if got fail.
$error['resize'][] = $this->image_lib->display_errors();
}
else
{
// otherwise, put each upload data to an array.
$success[] = $upload_data;
}
}
}
// see what we get
if(count($error > 0))
{
$data['error'] = $error;
}
else
{
$data['success'] = $upload_data;
}
redirect(base_url());
}
and here is my model
function add_car($data)
{
$this->db->insert('cars',$data);
}