Reputation:
I'm currently building a portfolio site for a client, and I'm having trouble with one small area. I want to be able to upload multiple images (varying number) inline for each portfolio item, and I can't see an obvious way to do it.
My view.php:
<?php echo form_open_multipart('uploadfile/upload');?>
<fieldset>
<div class="form-group">
<div class="row">
<div class="col-md-12">
<label for="filename[]" class="control-label">Select File to Upload</label>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-12">
<input type="file" name="filename" size="20" />
<span class="text-danger"><?php if (isset($error)) { echo $error; } ?></span>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-12">
<input type="submit" value="Upload File" class="btn btn-primary"/>
</div>
</div>
</div>
</fieldset>
<?php echo form_close(); ?>
<?php if (isset($success_msg)) { echo $success_msg; } ?>
</div>
my controller
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
ini_set("display_errors",1);
class Update_profile extends CI_Controller {
function __construct()
{
// Call the Model constructor
parent::__construct();
$this->is_login();
$this->load->helper(array('form', 'url'));
$this->load->model('Edit_profile');
}
public function index() {
// $this->load->view('header2');
$this->load->view('edit_profile');
}// index function ends
public function is_login() {
$is_login=$this->session->userdata('is_login');
if(!isset($is_login) || $is_login !=true)
{
//don't echo the message from controller
echo "you don't have permission to access this page <a href=../Homecontroller/index/>Login</a>";
die();
}
} //is_login function ends
// function to upload images
function upload()
{
$name_array = array();
$count = count($_FILES['filename']['size']);
foreach($_FILES as $key=>$value)
for($s=0; $s<=$count-1; $s++) {
$_FILES['filename']['name']=$value['name'][$s];
$_FILES['filename']['type'] = $value['type'][$s];
$_FILES['filename']['tmp_name'] = $value['tmp_name'][$s];
$_FILES['filename']['error'] = $value['error'][$s];
$_FILES['filename']['size'] = $value['size'][$s];
//set preferences
$config['remove_spaces']=TRUE;
// $config['encrypt_name'] = TRUE; // for encrypting the name
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'jpg|png|gif';
$config['max_size'] = '10248';
//load upload class library
$this->load->library('upload', $config);
if (!$this->upload->do_upload('filename'))
{
// case - failure
$upload_error = array('error' => $this->upload->display_errors());
$this->load->view('edit_profile', $upload_error);
}
else
{
// case - success
$upload_data = $this->upload->data();
$name_array[] = $data['file_name'];
$data['success_msg'] = '<div class="alert alert-success text-center">Your file <strong>' . $upload_data['file_name'] . '</strong> was successfully uploaded!</div>';
$this->load->view('edit_profile', $data);
}
}
}
function edit_profile() {
//echo "some success";
} //function edit profile ends
}
code above not working
Upvotes: 1
Views: 62
Reputation: 2257
This is what I've used for the last year when I've wanted/needed multiple image uploads in codeigniter:
https://github.com/stvnthomas/CodeIgniter-Multi-Upload
Upvotes: 1