【发布时间】:2021-03-30 03:46:50
【问题描述】:
我正在尝试为横幅图像添加多个图像,而为画廊图像添加多个图像,但是当我尝试添加图像时,它给了我错误:
数组到字符串对话。
我想将一张图片添加到同一张表中,将其他图片添加到不同的表中,这样当我将记录显示到我的视图中时,我可以轻松地制作一张横幅图片。
如果有人在这方面帮助我,我将非常感激。 所以这是我的看法。
<div class="col-lg-12">
<label for="main image">Upload Main Image: </label><br>
<input type="file" name="image_file">
</div><br><br><br>
<div class="col-lg-12">
<label for="Image name">Upload Mutiple Images:</label><br>
<input type="file" required name="image_name[]" multiple/><br>
</div>
// Model
function addnews($data, $id ='')
{
$title = $this->input->post('title');
$address = $this->input->post('address');
$city = $this->input->post('city');
$zip = $this->input->post('zipcode');
$type = $this->input->post('propertytype');
$status = $this->input->post('status');
$price = $this->input->post('price');
$description = $this->input->post('Description');
$userfile = $this->input->post('[image_file]');
$bedrooms = $this->input->post('bedrooms');
$rooms = $this->input->post('rooms');
$bath = $this->input->post('bathroom');
$garages = $this->input->post('gerages');
$date = $this->input->post('date');
$amenities = implode(',',$this->input->post('check'));
$w = array (
'title' => $title,
'address' => $address,
'city' => $city,
'zip' => $zip,
'type' => $type,
'status' => $status,
'description' => $description,
'bedrooms' => $bedrooms,
'rooms' => $rooms,
'price' => $price,
'userfile' => $data,
'bath' => $bath,
'date' => $date,
'garages' => $garages,
'amenities' => $amenities
);
$this->db->insert_batch("property", $w);
$id = $this->db->insert_id();
foreach ($data as $row) { // here data is from parameter
$data1[] = array(
'property_id' => $id, //Insert Inserted id
'image_name' => $row['file_name'] // this line is changed
);
}
}
// My controller.
public function addlisting()
{
$this->load->library('upload');
$config['upload_path']='./uploads';
$config['allowed_types']='*';
$this->upload->initialize($config);
$this->load->library('upload', $config);
if (!$this->upload->do_upload('image_file')) {
$this->session->set_flashdata('msg',"Failed To Apply. Please Check All The Fields.");
redirect(site_url('Welcome/add_listing'));
} else {
$fd = $this->upload->data();
$fn = $fd['file_name'];
//$this->User_model->addnews($fn);
$image = array();
$ImageCount = count($_FILES['image_name']['name']);
for ($i = 0; $i < $ImageCount; $i++) {
$_FILES['file']['name'] = $_FILES['image_name']['name'][$i];
$_FILES['file']['type'] = $_FILES['image_name']['type'][$i];
$_FILES['file']['tmp_name'] = $_FILES['image_name']['tmp_name'][$i];
$_FILES['file']['error'] = $_FILES['image_name']['error'][$i];
$_FILES['file']['size'] = $_FILES['image_name']['size'][$i];
// File upload configuration
$uploadPath = './uploads';
$config['upload_path'] = $uploadPath;
$config['allowed_types'] = 'jpg|jpeg|png|gif';
// Load and initialize upload library
$this->load->library('upload', $config);
$this->upload->initialize($config);
// Upload file to server
if ($this->upload->do_upload('file')) {
// Uploaded file data
$imageData = $this->upload->data();
$uploadImgData[$i]['file_name'] = $imageData['file_name'];
} else {
$this->session->set_flashdata('msg',"Error while uploading Data");
// $this->User_model->addnews($uploadImgData);
redirect(site_url('Welcome/add_listing'));
}
}
if (!empty($uploadImgData)) {
//Insert files data into the database
$this->User_model->addnews($uploadImgData);
$this->User_model->addnews($fn);
// $query = implode(",",$uploadImgData);
// $insert = $this->User_model->add($uploadImgData);
$this->session->set_flashdata('msg',"Property Uploaded successfully");
redirect('Welcome/add_listing');
}
}
【问题讨论】:
标签: php image codeigniter upload