我正在建立一个让用户出售汽车的网站。用户在表单中填写必要的字段,其中一个必填字段正在上传该特定汽车的图像。我的代码可以正常工作,预计当用户上传多个图像时,我将有两个具有相似值的列。上传的图像和carID,这使我很难检索图像。如果有人知道更好的方式来正确存储这些图像并检索它们我会很感激。这是我的数据库结构:
CarID | want_to |制作|型号|年份|公里|传输|位置|价格|手机|图片
这是我的控制器:
// 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());
}
这是我的模特
function add_car($data)
{
$this->db->insert('cars',$data);
}
答案 0 :(得分:1)
首先要做的只是创建一个图像表,其中carid作为图像表中的外键。我家的carid是汽车表的主要关键吗?如果没有,那应该是。
在您要插入汽车数据的模型中,您可以在插入后使用$ this-> db-> insert_id();功能见下面的代码。
function add_car($data)
{
$this->db->insert('cars',$data);
return $this->db->insert_id(); // it returns primary key value it should be carid.
}
现在在控制器上传图像后,只需使用add_car()的返回值将其插入images文件夹中。
希望它对你有所帮助!!如果不是我很乐意帮助你!!
答案 1 :(得分:0)
将图像存储在另一个表中,carid
作为上述表中引用carid
的外键。在检索两个表的连接时。不要在表格中存储冗余数据。