如何在Codeigniter中多次上传图像



我正在尝试将照片多次上传到Codeigniter 3中的表单中。当我想上传照片时,点击上传会向我发送整个表单,并且只添加一张照片。如果有人能给我一些如何做到这一点的建议,我将不胜感激。我可以把它作为两种不同的形式,但它并不像我想的那样起作用。

我的表单视图

<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 );

}

您应该更改

if($this->upload->do_upload('file')){ 

进入

if($this->upload->do_upload('files')){

未来参考查看链接了解如何在codeigniter中上传多个文件代码点火器中的多次上传

在这个例子中,您有一个图像上传和多个图像上传不同的文件夹。

if($param=="upload"({

$config['upload_path']          = './uploads/user_profil/';
$config['allowed_types']        = 'gif|jpg|png';
$config['max_size']             = 15000;
$config['file_name']             = $this->input->post('user_profile')."_profil_image_".mt_rand(100000,900000);
$this->load->library('upload', $config, 'user_profile'); // Create custom object for cover upload
$this->user_profile->initialize($config);

if ( !empty($this->user_profile->do_upload('user_profil_image') ) )
{
$error = array('error' => $this->user_profile->display_errors());
$file_name_profile_picture = $this->input->post('user_profil_image_actual');
$upload_data = $this->user_profile->data(); 
$file_name_profile_picture = $upload_data['file_name'];
}
else
{           
$file_name_profile_picture ="";
} 
//map photos
$config_galery['upload_path']          = './uploads/map_profile/';
$config_galery['allowed_types']        = 'gif|jpg|png';
$config_galery['max_size']             = 15000;
$config_galery['file_name']             = $this->input->post('name')."_user_profilm_map_".mt_rand(100000,900000);

$this->load->library('upload', $config_galery, 'map_profile'); // Create custom object for cover upload
$this->map_profile->initialize($config_galery);

if ( !empty($this->map_profile->do_upload('map_profile_image'))  )
{
$error = array('error' => $this->map_profile->display_errors()); 
$map_profile_image =$this->input->post('map_profile_image_actual');
$upload_data1 = $this->map_profile->data(); 
$map_profile_image = $upload_data1['file_name'];

} else {
$map_profile_image="";
}

if(!empty($_FILES['userfile']['name'])){ 
$b = $this->doupload_gallery(); 
$galery_photo_links =  json_encode(json_decode($b, true));
} else  {
// default empty state
$galery_photo_links ="[]";
}  

if (!empty($this->input->post('profile_qr_id'))) {
$data_update_qr_status = array(
'statusi' => 1,
'table_name' => "table_name_db"
); 
$this->db->where('profile_qr_id', $this->input->post('profile_qr_id'));
$this->db->update('table_name', $data_update_qr_status);  

} 
$this->db->insert('table_name', $data); 
// ridirect      
redirect(base_url('admin/uploads_view'), 'refresh');        

}

相关内容

  • 没有找到相关文章

最新更新