How to upload multiple files in codeigniter 3.0.1. There are similar issues and solutions in stackoverflow but unfortunately non of them are helping to fix the issue I am facing.
This is the error message appearing You did not select a file to upload with my current code
view (addGallery)
<section>
<h2>Add Gallery</h2>
<?php echo form_open('Newsupload/gallery', ['id'=>'news', 'name'=>'news', 'method'=>'post','enctype'=>'multipart/form-data']) ?>
<div class="grp width-50">
<label for="name">Album Name</label>
<input type="text" name="name" id="name" value="" placeholder="">
</div>
<div class="grp width-100">
<div id="selectedFiles"></div>
<input type="file" id="files" name="files[]" multiple size="20"><br/>
</div>
<?php if (isset($error)) {
echo $error;
} ?>
<grp class="grp width-100">
<button>Add</button>
</grp>
</form>
</section>
controller (gallery)
public function gallery()
{
$this->load->library('upload');
$files = $_FILES;
$cpt = count($_FILES['files']['name']);
for($i=0; $i<$cpt; $i++)
{
$_FILES['files']['name']= $files['files']['name'][$i];
$_FILES['files']['type']= $files['files']['type'][$i];
$_FILES['files']['tmp_name']= $files['files']['tmp_name'][$i];
$_FILES['files']['error']= $files['files']['error'][$i];
$_FILES['files']['size']= $files['files']['size'][$i];
$this->upload->initialize($this->set_upload_options());
// $this->upload->do_upload('files[]');
if (!$this->upload->do_upload('files[]'))
{
$error =['error' => $this->upload->display_errors()];
$this->load->view('admin/addGallery', $error);
}
}
}
public function set_upload_options()
{
$config['upload_path'] = getcwd().'/upload/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['remove_spaces'] = true;
return $config;
}
By default codeIgniter doesn't support multi-file upload. So you can use
this library CodeIgniter Multiple Upload Library
I think you need to change this line:
if (!$this->upload->do_upload('files[]'))
to
if (!$this->upload->do_upload('files'))
you did not pass file name to upload function.Try this.
if (!$this->upload->do_upload($_FILES['files']['name']))
{
$error =['error' => $this->upload->display_errors()];
$this->load->view('admin/addGallery', $error);
}
try this:
$files = $_FILES;
$cpt = count($_FILES['files']['name']);
for($i=0; $i<$cpt; $i++)
{
$_FILES['files']['name']= $files['files']['name'][$i];
$_FILES['files']['type']= $files['files']['type'][$i];
$_FILES['files']['tmp_name']= $files['files']['tmp_name'][$i];
$_FILES['files']['error']= $files['files']['error'][$i];
$_FILES['files']['size']= $files['files']['size'][$i];
$this->upload->initialize($this->set_upload_options());
$this->upload->do_upload();
$fileName = $_FILES['files']['name'];
$images[] = $fileName;
and make a function set_upload_options()
$config = array();
$config['upload_path'] = './upload/'; //give the path to upload the image in folder
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['overwrite'] = FALSE;
return $config;
For more Upload multiple files using codeigniter try this http://w3code.in/2015/09/upload-file-using-codeigniter/
Please try bellow
for($i=0; $i<$cpt; $i++)
{
$_FILES['files'] = $files[$i];
$this->upload->initialize($this->set_upload_options());
if (!$this->upload->do_upload('files'))
{
$error =['error' => $this->upload->display_errors()];
$this->load->view('admin/addGallery', $error);
}
}
You can upload multiple files with CodeIgniter in a single request. You just need to do do_upload() for each file. Here's one implementation. You can work it into a loop if needed.
// Image upload Config
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '1000000';
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
// Upload Files
if ( $_FILES['first_file']['name'] && $this->upload->do_upload('first_file') ) {
$first_file = $this->upload->data();
}
if ( $_FILES['second_file']['name'] && $this->upload->do_upload('second_file') ) {
$second_file= $this->upload->data();
}
Just that i understood from the library $this->upload->do_upload('inputelementname') doesn't have to be input element name. It should be the key of files array.
So in this context this should work
if($_FILES){
$filecount=count($_FILES['addmediaelement']['name']);
for($i=0; $i<$filecount; $i++){
$_FILES['mediaelement']['name']=$_FILES['addmediaelement']['name'][$i];
$_FILES['mediaelement']['type']=$_FILES['addmediaelement']['type'][$i];
$_FILES['mediaelement']['tmp_name']=$_FILES['addmediaelement']['tmp_name'][$i];
$_FILES['mediaelement']['error']=$_FILES['addmediaelement']['error'][$i];
$_FILES['mediaelement']['size']=$_FILES['addmediaelement']['size'][$i];
$config['upload_path']=path/to/save/file;
$config['file_name']=filealternatename.extension;
$config['max_size']=MAXSIZE; //max size constant
$config['max_width']=MAXWIDTH; //max width constant
$config['max_height']=CMPMAXHEIGHT; //max height constant
$config['allowed_types']='jpg|png';
if(!is_dir($config['upload_path'])){
mkdir($config['upload_path'], 0775);
}
$this->upload->initialize($config);
$imageuploaderres[]=$this->upload->do_upload('mediaelement');
}
}
Related
Here I am attaching the code of the desires problem.
Cotroller has following code.
Controller=>
//Load upload library
$this->load->library('upload');
$images = array();
$i = 0;
foreach ($_FILES as $key => $value)
{
$tmp = explode(".",$value['name'][$i]);
$imagename = time().".".end($tmp);
$_FILES['file']['name'] = $imagename;
$_FILES['file']['type'] = $value['type'][$i];
$_FILES['file']['tmp_name'] = $value['tmp_name'][$i];
$_FILES['file']['error'] = $value['error'][$i];
$_FILES['file']['size'] = $value['size'][$i];
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name'] = $imagename;
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('file'))
{
$error = array($i => $this->upload->display_errors());
echo "<pre>";print_r($error);die;
}
else
{
array_push($images,$this->upload->data()['file_name']);
}
$i++;
}
echo "<pre>";print_r($images);die;
This is a form code that I am using while uploading file.
View =>
<?php $attributes = array(
"class" => "form-horizontal m-t-20",
"method" => "post",
"novalidate" => "",
"enctype" => "multipart/form-data"
);
echo form_open('admin/user/adduser', $attributes); ?>
Here is my file input control.
<label for="file">Profile Images*</label>
<input type="file" name="files[]" id="file" multiple required placeholder="Profile Images" class="form-control">
Change your code as follows
foreach($_FILES["files"]["tmp_name"] as $key=>$value) {
and change $i to the $key as follows (apply to the all)
$_FILES['file']['type'] = $_FILES["files"]['type'][$key];
As wazabii suggested, attached some random string to the file name. You can use rand(100,10000)
That is because the time() will be the same on all the images, so the file name is not unique. This is easily fixed by adding the array key to the file name.
$tmp = explode(".",$value['name'][$i]);
$imagename = time()."-".$key.".".end($tmp);
contoller code
public function upload_multiple($field_name,$path){
$this->load->library('upload');
$files = $_FILES;
$cpt = count($_FILES[$field_name]['name']);//count for number of image files
$image_name =array();
for($i=0; $i<$cpt; $i++)
{
$_FILES[$field_name]['name']= $files[$field_name]['name'][$i];
$_FILES[$field_name]['type']= $files[$field_name]['type'][$i];
$_FILES[$field_name]['tmp_name'] = $files[$field_name]['tmp_name'][$i];
$_FILES[$field_name]['error']= $files[$field_name]['error'][$i];
$_FILES[$field_name]['size'] = $files[$field_name]['size'][$i];
$this->upload->initialize($this->set_upload_options($path));//for initalizing configuration for each image
$this->upload->do_upload($field_name);
$data = array('upload_data' => $this->upload->data());
$image_name[]=$data['upload_data']['file_name'];//store file name to store in database
}
return $image_name;//all images name which is uploaded
}
public function set_upload_options($path)
{
$config = array();
$config['upload_path'] = $path;
$config['allowed_types'] = 'gif|jpg|png';
$config['overwrite'] = FALSE;
return $config;
}
function call
$image_name=$this->upload_multiple('portfolio_image',$path);//for multiple image upload
input field
<input type="file" id="portfolio_image" name="protfolio_image[]" >
Following is my controller In this I am using two if statements one for multiple images and another is for the featured image.. my images are uploaded in a folder very well but multiple names are not inserted in the database...Only one file name is inserted in the database...
public function uploadApi()
{
if (isset($_FILES['userfile'])) {
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 200000;
$config['max_width'] = 2024;
$config['max_height'] = 1768;
$this->upload->initialize($config);
$this->load->library('upload', $config);
$this->upload->do_upload('userfile');
$data = array( $this->upload->data());
$this->m->update_post($data[0]['file_name']);
}
if(isset($_FILES['userfile1'])) {
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 200000;
$config['max_width'] = 2024;
$config['max_height'] = 1768;
$this->upload->initialize($config);
$this->load->library('upload', $config);
$this->upload->do_upload('userfile1');
$data = array( $this->upload->data());
$this->m->update_feature($data[0]['file_name']);
}
}
This is a model ..
#-Update images Post-#
public function update_post($picture) {
$post = array(
'post_images'=>$picture,
);
$this->db
->where('post_status','draft')
->update('post',$post);
return true;
}
public function update_feature($picture) {
$post = array(
'post_featured_image'=>$picture,
);
$this->db
// ->set('post_created', 'NOW()', FALSE)
->where('post_status','draft')
->update('post',$post);
return true;
}
filepond plugin script
FilePond.registerPlugin(
FilePondPluginFileValidateSize,
FilePondPluginImageExifOrientation,
FilePondPluginImageCrop,
FilePondPluginImageResize,
FilePondPluginImagePreview,
FilePondPluginImageTransform
);
// Set default FilePond options
FilePond.setOptions({
// maximum allowed file size
maxFileSize: '50MB',
imagePreviewHeight: 100,
imagePreviewWidth: 200,
instantUpload: true,
// crop the image to a 1:1 ratio
imageCropAspectRatio: '1:1',
// upload to this server end point
server: {
url: '<?php echo base_url() ?>Admin/uploadApi',
}
});
var pond = FilePond.create(document.querySelector('input[name="userfile"]'));
var pond = FilePond.create(document.querySelector('input[name="userfile1"]'));
**This is a view ..**
<form method="post" enctype="multipart/form-data" class="toggle-disabled" action="<?php echo base_url() ?>Admin/update_post1" id='ritesh'>
<div class="col-md-6">
<div class="form-group">
<label>Upload images</label>
<input type="file"
class="filepond"
name="userfile"
multiple
data-max-file-size="5MB"
data-max-files="50" data-validation="required extension" />
</div>
<div class="form-group">
<label>Feature image</label>
<input type="file"
class="filepond"
name="userfile1"
data-max-file-size="5MB"
data-validation="required extension"
/>
</div>
</form>
For multiple image upload you should post images array like; imagename[]. Your current approach is not good.
You must try already posted answers:
Multiple image upload with CodeIgniter
Multiple image upload with Codeigniter saving only one file path to MySQL Database
https://www.codexworld.com/codeigniter-upload-multiple-files-images/
Please try to this in controller
function uploadApi() {
$image = $_FILES;
foreach ($image as $key => $img) {
if (!is_dir('./Uploads/')) {
mkdir('./Uploads/', 0777, TRUE);
}
if (!empty($img['name'])) {
$config['upload_path'] = './Uploads/Products/';
$config['allowed_types'] = '*';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['overwrite'] = TRUE;
$config['file_name'] = date('U') . '_' . $img['name'];
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload($key)) {
$error = array('error' => $this->upload->display_errors());
print_r($error);
die;
} else {
if ($this->upload->do_upload($key)) {
$image_data = $this->upload->data();
$update["userfile"] = $config['file_name'];
$res = $this->m->update_post($update);
}
}
}
}
$this->load->view('imgtest');
}
I am working on codeigniter and during creation of one of the APIs I got the issue. I tried to upload the image on Server as a file, while searching on the web, I got familiar with inbuild upload class in codeigniter. Please have a look at this code. I am sending file from Android using this tutorial.
public function upload_image_post(){
$config['upload_path'] =base_url().'/uploads/';
$config['file_name'] = rand() .'.jpg';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = 10000;
$config['remove_spaces'] = TRUE;
$config['encrypt_name'] = TRUE;
// $file = $this->input->post('file');
$this->load->library('upload', $config);
// $this->upload->initialize($config);
$file=$_FILES['uploaded_file'];
// $this->upload->do_upload($file);
if($file){
$content=array(
'image_id'=>'IMG'.rand(),
'album_id'=> 'A',
'sp_id'=>'asQ',
'image_name'=>'aAA',
'status'=>1,
'tags'=>'s');
/* This is working*/
$res = $this->db->insert('ww_portfolio_images',$content);
}else{
$content=array(
'image_id'=>'IMG'.rand(),
'album_id'=> 'not file',
'sp_id'=>'asQaaaaa',
'image_name'=>'aAA',
'status'=>1,
'tags'=>'s');
/* This is not working, Thats Obvious*/
$res = $this->db->insert('ww_portfolio_images',$content);
}
// $destinationPath=APPPATH.'public/assets/uploads/ANKO.jpg';
if($this->upload->do_upload('uploaded_file')) {
$content=array(
'image_id'=>'IMG'.rand(),
'album_id'=> 'A',
'sp_id'=>'aaaaaaaaaaaaaaaaas',
'image_name'=>'aAA',
'status'=>1,
'tags'=>'s');
/* This is not working*/
$res = $this->db->insert('ww_portfolio_images',$content);
$this->response(['result' =>'Success',] , REST_Controller::HTTP_OK);
// return ($arr_image_info['full_path']);
}
else{
$content=array(
'image_id'=>'IMG'.rand(),
'album_id'=> 'A',
'sp_id'=>'asass',
'image_name'=>'aAA',
'status'=>1,
'tags'=>'s');
$res = $this->db->insert('ww_portfolio_images',$content);
/* This is working*/
$this->response(['result' => 'ERrro'] , REST_Controller::HTTP_OK);
// $this->response(['result' =>'Image error',], 433);
}
}
I can not figure out the problem I am facing here. I am receiving a file but it does not upload.
I have also tried to use $this->upload->do_upload() instead of $this->upload->do_upload('uploaded_file') and this $config['max_size'] = '10000'; instead of this $config['max_size'] = 10000; . Please help. Any help would be greatly appreciated.
Also, when this code run through web panel, it working fine.
Better if you provide some more detail for the type of error or warning that you are observing.
There could be number of reasons.
1) base_url() gives you the public URL. You have to specify the absolute or relative path to your upload folder.
2) (if you are using an Apache Server) Your Apache user don't have the write permission to the upload folder.
3) Folder path doesn't exists.
If the first one doesn't work, please check the rest of the points. Hope this help you.
Regards
Muaaz
Try using FCPATH
$config['upload_path'] = FCPATH . '/uploads/';
Or
$config['upload_path'] = './uploads/';
Then http://www.codeigniter.com/user_guide/libraries/file_uploading.html#the-controller
<?php
class Example extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('form');
}
// Name function what every you want remember to change it on view form.
public function upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ($this->upload->do_upload('uploaded_file')) {
// Then the success stuff
$upload_data = $this->upload->data();
echo $upload_data['file_name'];
} else {
// Errors
}
}
}
On view I would use the form helper functions form_open_multipart()
<?php echo form_open_multipart('example/upload');?>
<?php echo form_upload('uploaded_file', 'Upload');?>
<?php echo form_close();?>
check your form input in the view
My form view :
<input id="document" type="file" data-browse-label="browse" name="document" data-show-upload="false" data-show-preview="false" class="form-control file" />
other can be
permission issue for the uploads folder
if the folder does not exist you have to create it
And always try to debug the code with logs
document in the do_upload is the name of the input element in my view
if ($_FILES['document']['size'] > 0) {
$this->load->library('upload');
$config['upload_path'] = 'uploads/images';
$config['allowed_types'] = '*';
$config['max_size'] = $this->allowed_file_size;
$config['overwrite'] = false;
$config['encrypt_name'] = true;
$this->upload->initialize($config);
if (!$this->upload->do_upload('document')) {
$error = $this->upload->display_errors();
$this->session->set_flashdata('error', $error);
redirect($_SERVER["HTTP_REFERER"]);
}
$photo = $this->upload->file_name;
}
I am new user to using code igniter in my project, I am facing one problem while uploading multiple files but the last one only insert to all image three images field.
my controller is:
function products()
{
date_default_timezone_set("Asia/Kolkata");
$config['upload_path'] = './resources/images/products/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 1000;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
$this->upload->do_upload('userfile');
$data = array('prod_image' => $this->upload->data(),
'prod_image1' => $this->upload->data(),
'prod_image2' => $this->upload->data());
$product_image=$data['prod_image']['file_name'];
$product_image1=$data['prod_image1']['file_name'];
$product_image2=$data['prod_image2']['file_name'];
$data = array(
'name' => $this->input->post('pd_name'),
'prod_image' => $product_image,
'prod_image1' => $product_image1,
'prod_image2' => $product_image2,
'created_time' => date('Y-m-d H:i:s'));
// insert form data into database
$result_set= $this->tbl_products_model->insertUser($data);
}
my view part is:
<input class="form-control" name="pd_name"type="text"/>
<input type="file" class="file_upload2" name="userfile"/> //1
<input type="file" class="file_upload2" name="userfile"/> //2
<input type="file" class="file_upload2" name="userfile"/>//3
Please help how to insert 3 images.
my datad base like
===========================================
id|name|prod_image|prod_image1|prod_image2|
===========================================
1|ard| | | |
============================================
Html :
<input type="file" name="userfile[]" multiple="multiple">
PHP :
<?php
public function products()
{
$this->load->library('upload');
$dataInfo = array();
$files = $_FILES;
$cpt = count($_FILES['userfile']['name']);
for($i=0; $i<$cpt; $i++)
{
$_FILES['userfile']['name']= $files['userfile']['name'][$i];
$_FILES['userfile']['type']= $files['userfile']['type'][$i];
$_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
$_FILES['userfile']['error']= $files['userfile']['error'][$i];
$_FILES['userfile']['size']= $files['userfile']['size'][$i];
$this->upload->initialize($this->set_upload_options());
$this->upload->do_upload('userfile');
$dataInfo[] = $this->upload->data();
}
$data = array(
'name' => $this->input->post('pd_name'),
'prod_image' => $dataInfo[0]['file_name'],
'prod_image1' => $dataInfo[1]['file_name'],
'prod_image2' => $dataInfo[2]['file_name'],
'created_time' => date('Y-m-d H:i:s')
);
$result_set = $this->tbl_products_model->insertUser($data);
}
private function set_upload_options()
{
//upload an image options
$config = array();
$config['upload_path'] = './resources/images/products/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['overwrite'] = FALSE;
return $config;
}
?>
Multiple files uploads unlimited files
Database table(profile_images) column names are image_name(255,varcher), added_datetime(current timestamp)
View
<?php echo validation_errors();?>
<?php echo form_open_multipart('pages/multiple_files');?>
<p><input type="file" multiple="multiple" name="image_name[]" class="form-control" /></p>
<input type="submit" class="btn btn-success btn-block"/>
</form>
Controller
public function multiple_files(){
$this->load->library('upload');
$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 = './assets/images/profiles/';
$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]['image_name'] = $imageData['file_name'];
}
}
if(!empty($uploadImgData)){
// Insert files data into the database
$this->pages_model->multiple_images($uploadImgData);
}
}
Model
public function multiple_images($image = array()){
return $this->db->insert_batch('profile_images',$image);
}
The problem is with the following line of code:
<input type="file" class="file_upload2" name="userfile"/> //1
<input type="file" class="file_upload2" name="userfile"/> //2
<input type="file" class="file_upload2" name="userfile"/>//3
These all three have same name.
To solve this there are two ways:
Give diff name to all 3 input type file
Make a single input type file with its multiple file selection true and its name must be an array like:
<input type="file" name="filefield[]" multiple="multiple">
Make the following changes and try again.
code of controller.
public function upload_multiple($field_name,$path){
$this->load->library('upload');
$files = $_FILES;
$cpt = count($_FILES[$field_name]['name']);//count for number of image files
$image_name =array();
for($i=0; $i<$cpt; $i++)
{
$_FILES[$field_name]['name']= $files[$field_name]['name'][$i];
$_FILES[$field_name]['type']= $files[$field_name]['type'][$i];
$_FILES[$field_name]['tmp_name'] = $files[$field_name]['tmp_name'][$i];
$_FILES[$field_name]['error']= $files[$field_name]['error'][$i];
$_FILES[$field_name]['size'] = $files[$field_name]['size'][$i];
$this->upload->initialize($this->set_upload_options($path));
//for initalizing configuration for each image
$this->upload->do_upload($field_name);
$data = array('upload_data' => $this->upload->data());
$image_name[]=$data['upload_data']['file_name'];
//store file name into array
}
return $image_name;//all images name which is uploaded
}
public function set_upload_options($path)
{
$config = array();
$config['upload_path'] = $path;
$config['allowed_types'] = '*';
$config['overwrite'] = FALSE;
return $config;
}
call function
$image_name=$this->upload_multiple('profile_image',$path=USER_OTHER);//we get all name of uploaded file in $image_name array.
My functions upload only one image at a time, when the form is submitted. I can not upload multiple images at once. This is a huge problem because I am building a car-sales website, and people need to upload multiple car images.
My upload.php controller:
<?php
class Upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->model('upload_model');
}
function index()
{
$this->load->view('common/header');
$this->load->view('nav/top_nav');
$this->load->view('upload_form', array('error' => ' ' ));
}
function do_upload()
{
if($this->input->post('upload'))
{
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1024';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data=$this->upload->data();
$this->thumb($data);
$file=array(
'img_name'=>$data['raw_name'],
'thumb_name'=>$data['raw_name'].'_thumb',
'ext'=>$data['file_ext'],
'upload_date'=>time()
);
$this->upload_model->add_image($file);
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
else
{
redirect(site_url('upload'));
}
}
function thumb($data)
{
$config['image_library'] = 'gd2';
$config['source_image'] =$data['full_path'];
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 160;
$config['height'] = 110;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
}
My upload_form.php view:
<?php $attributes = array('name' => 'myform');
echo form_open_multipart('/upload/do_upload',$attributes);?>
<input type="file" name="userfile" size="20" />
<input type="submit" value="upload" name="upload" />
<?php echo form_close(); ?>
My upload_model.php Model:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Upload_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
function add_image($data)
{
$this->db->insert('jobs',$data);
}
}
I can't modify the function in a way that allows multiple image upload. I would highly appreciate any kind of guidance or help. Thank you in advance!
#####################
# Uploading multiple#
# Images #
#####################
$files = $_FILES;
$count = count($_FILES['uploadfile']['name']);
for($i=0; $i<$count; $i++)
{
$_FILES['uploadfile']['name']= $files['uploadfile']['name'][$i];
$_FILES['uploadfile']['type']= $files['uploadfile']['type'][$i];
$_FILES['uploadfile']['tmp_name']= $files['uploadfile']['tmp_name'][$i];
$_FILES['uploadfile']['error']= $files['uploadfile']['error'][$i];
$_FILES['uploadfile']['size']= $files['uploadfile']['size'][$i];
$this->upload->initialize($this->set_upload_options());//function defination below
$this->upload->do_upload('uploadfile');
$upload_data = $this->upload->data();
$name_array[] = $upload_data['file_name'];
$fileName = $upload_data['file_name'];
$images[] = $fileName;
}
$fileName = $images;
what's happening in code??
well $_FILE---->it is an associative array of items uploaded to the current script via the POST method.for further look this LINK
it's an automatic variable avaliable within all scopes of script
function set_upload_options()
{
// upload an image options
$config = array();
$config['upload_path'] = LARGEPATH; //give the path to upload the image in folder
$config['remove_spaces']=TRUE;
$config['encrypt_name'] = TRUE; // for encrypting the name
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '78000';
$config['overwrite'] = FALSE;
return $config;
}
and in your html markup don't don't forget:
Input name must be be defined as an array i.e. name="file[]"
Input element must have multiple="multiple" or just multiple
3.$this->load->library('upload'); //to load library
4.The callback, $this->upload->do_upload() will upload the file selected in the given field name to the destination folder.
5.And the callback $this->upload->data() returns an array of data related to the uploaded file like the file name, path, size etc.