Я пытаюсь сделать несколько загрузок фотографий в форму в Codeigniter 3. Когда я хочу загрузить фотографии, нажатие кнопки «Upload» отправляет мне всю форму и добавляет только одну фотографию. Если бы кто-нибудь мог дать мне советы, как это сделать, я был бы благодарен. Я мог бы сделать это как две отдельные формы, но тогда это не сработало бы так, как мне хотелось бы.
Мой вид формы
<form method='post' action='<?php echo base_url();?>ads/create' enctype='multipart/form-data'>
<div class="form-group row">
<label for="email" class="col-4 col-form-label text-uppercase text-right">Tytuł :</label>
<div class="col-8">
<input type="text" class="form-control" name="title" value="<?php echo set_value('title'); ?>">
</div>
</div>
<div class="form-group row">
<label for="message" class="col-4 col-form-label text-uppercase text-right">Opis ogłoszenia:</label>
<div class="col-8">
<textarea name="description" id="message" cols="70" rows="5"
class="form-control" ><?php echo set_value('description'); ?></textarea>
</div>
</div>
<div class="form-group row">
<label for="contact" class="col-4 col-form-label text-uppercase text-right">Osoba do kontaktu:</label>
<div class="col-8">
<input type="text" class="form-control" name="contact" value="<?php echo set_value('contact'); ?>">
</div>
</div>
<div class="form-group row">
<label for="email" class="col-4 col-form-label text-uppercase text-right">Adres email:</label>
<div class="col-8">
<input type="text" class="form-control" name="email" <?php if ( logged_in() == true )
{ echo "value='".$user->email."' disabled"; } ?> >
</div>
</div>
<div class="form-group row">
<label for="phone" class="col-4 col-form-label text-uppercase text-right">Numer telefonu:</label>
<div class="col-8">
<input type="text" class="form-control" name="phone" value="<?php echo set_value('phone'); ?>">
</div>
</div>
<div class="form-group row">
<label for="message" class="col-4 col-form-label text-uppercase text-right">Zdjęcia:</label>
</div>
<hr>
<input type='file' name='files[]' multiple=""> <br/><br/>
<input id="submit" type='submit' value='Upload' name='upload' />
<strong><?php if(isset($totalFiles)) echo "Successfully uploaded ".count($totalFiles)." files"; ?></strong>
<div class="form-group row">
<div class="col-12 text-center">
<button class="btn btn-primary w-50 mt-3">Dodaj</button>
</div>
</div>
<?php echo form_close(); ?>
И мой контроллер
public function create()
{
$user_id = $this->session->userdata( 'id' );
$where = array( 'id' => $user_id);
$user = $this->Site_model->get_single('users', $where);
$data['user'] = $user;
if ( !empty( $_POST ) )
{
if ( $this->form_validation->run( 'site_ads_create' ) == true )
{
if ( logged_in() == true )
{
$data = array(
'email' => $this->session->userdata( 'email' ),
'title' => $this->input->post( 'title' , true ),
'description' => $this->input->post( 'description' , true ),
'category_id' => $this->input->post( 'category_id' , true ),
'city_id' => $this->input->post( 'city_id' , true ),
'price' => $this->input->post( 'price' , true ),
'contact' => $this->input->post( 'contact' , true ),
'phone' => $this->input->post( 'phone' , true ),
'user_ip' => getUserIpAddr(),
'created' => time(),
'active' => 1,
);
}
else
{
$data = array(
'title' => $this->input->post( 'title' , true ),
'description' => $this->input->post( 'description' , true ),
'category_id' => $this->input->post( 'category_id' , true ),
'city_id' => $this->input->post( 'city_id' , true ),
'price' => $this->input->post( 'price' , true ),
'contact' => $this->input->post( 'contact' , true ),
'email' => $this->input->post( 'email' , true ),
'phone' => $this->input->post( 'phone' , true ),
'user_ip' => getUserIpAddr(),
'created' => time(),
'active' => 0,
);
}
$count = count($_FILES['files']['name']);
for($i=0;$i<$count;$i++){
if(!empty($_FILES['files']['name'][$i])){
$_FILES['file']['name'] = $_FILES['files']['name'][$i];
$_FILES['file']['type'] = $_FILES['files']['type'][$i];
$_FILES['file']['tmp_name'] = $_FILES['files']['tmp_name'][$i];
$_FILES['file']['error'] = $_FILES['files']['error'][$i];
$_FILES['file']['size'] = $_FILES['files']['size'][$i];
$config['upload_path'] = 'images/';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['max_size'] = '5000';
$config['file_name'] = $this->input->post( 'title' , true );
$this->load->library('upload',$config);
if($this->upload->do_upload('file')){
$uploadData = $this->upload->data();
$filename = $uploadData['file_name'];
$data['totalFiles'][] = $filename;
}
}
}
$this->Site_model->create( 'ads' , $data );
$ad_id = $this->Site_model->last_id();
if ($this->input->post('promo' , true ) == 'tak' )
{
$this->session->set_userdata('promo_id', $ad->id);
redirect( 'ads/promo' );
}
$this->session->set_flashdata( 'alert' , 'Ad has been added.' );
//refresh();
}
else
{
$this->session->set_flashdata( 'alert' , validation_errors() );
//refresh();
}
}
$data['cities'] = $this->Site_model->get_cities('cities', 'name', 'asc');
$data['categories'] = $this->Site_model->get_categories();
$this->load->view( 'create' , $data );
}