How to insert into database 3 arrays files on 3 field - codeigniter

i have a problem in my try project on upload multiple image.
I can't just use a fixed number of file to upload. I tried many many solutions on StackOverflow but I wasn't able to find a working one..
my table format on database :
enter image description here
Here's my Upload controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Upload2 extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('url','html','form'));
$this->load->model('m_upload');
}
function index(){
$this->load->view('upload_form');
}
function upload() {
if($this->input->post('upload'))
{
$foto = array();
$number_of_files = sizeof($_FILES['userfiles']['tmp_name']);
$files = $_FILES['userfiles'];
$config=array(
'upload_path' => './uploads/', //direktori untuk menyimpan gambar
'allowed_types' => 'jpg|jpeg|png|gif',
'max_size' => '2000',
'max_width' => '2000',
'max_height' => '2000'
);
for ($i = 0;$i < $number_of_files; $i++)
{
$_FILES['userfile']['name'] = $files['name'][$i];
$_FILES['userfile']['type'] = $files['type'][$i];
$_FILES['userfile']['tmp_name'] = $files['tmp_name'][$i];
$_FILES['userfile']['error'] = $files['error'][$i];
$_FILES['userfile']['size'] = $files['size'][$i];
$this->load->library('upload', $config);
$this->upload->do_upload('userfile');
$foto[] = $this->upload->data();
$data = array(
//$data[$parts[0]] = isset($parts[1]) ? $parts[1] : null;
'foto' => $foto[0]['file_name'],
'foto_ktp' => $foto[1]['file_name'],
'foto_npwp' => $foto[2]['file_name']
);
//$this->m_upload->m_upload($data);
$result_set = $this->m_upload->insert($data);
}
}
$this->load->view('upload_success');
}
}
My upload form is This.
<!DOCTYPE html>
<html>
<head>
<title>Tutorial CodeIgniter with Gun Gun Priatna</title>
</head>
<body>
<h2>Upload Gambar</h2>
<?php echo form_open_multipart('index.php/upload2/upload'); ?>
<table>
<tr>
<td>FILE 1<input type="file" name="userfiles[0]" /></td>
<td>FILE 2<input type="file" name="userfiles[1]" /></td>
<td>FILE 3<input type="file" name="userfiles[2]" /></td>
</tr>
<tr>
<td><input type="submit" name="upload" value="upload"></td>
</tr>
</table>
<?php echo form_close();?>
</body>
</html>
how to fix insert 3 file into database above..thank u very much..

You are inserting in the loop, you want to insert after the loop when you have all the files.
PHP:
function upload() {
if (!empty($_FILES['userfiles']['name'])) {
$foto = array();
$number_of_files = count($_FILES['userfiles']['tmp_name']);
$files = $_FILES['userfiles'];
$config = array(
'upload_path' => './uploads/', //direktori untuk menyimpan gambar
'allowed_types' => 'jpg|jpeg|png|gif',
'max_size' => '2000',
'max_width' => '2000',
'max_height' => '2000'
);
$this->load->library('upload', $config);
$foto = array();
for ($i = 0; $i < $number_of_files; $i++) {
$_FILES['userfile']['name'] = $files['name'][$i];
$_FILES['userfile']['type'] = $files['type'][$i];
$_FILES['userfile']['tmp_name'] = $files['tmp_name'][$i];
$_FILES['userfile']['error'] = $files['error'][$i];
$_FILES['userfile']['size'] = $files['size'][$i];
if (!$this->upload->do_upload('userfile')) {
show_error($this->upload->display_errors());
}
$foto[] = $this->upload->data('file_name');
}
$data = array(
'foto' => $foto[0],
'foto_ktp' => $foto[1],
'foto_npwp' => $foto[2]
);
$this->m_upload->insert($data);
$this->load->view('upload_success');
} else {
show_error('No files uploaded!');
}
}
HTML:
No need for userfiles[2] just call them userfiles[]

Related

upload multiple pictures in codeigniter

HTML Code
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="file1" size="20" />
<input type="file" name="file2" size="20" />
<input type="submit" value="upload" />
</form>
Controller code
public function do_upload()
{
$pic1 = "Name One";
$pic2 = "Name Two";
$RealName = array('file1', 'file2' );
$ChangeName = array($pic1, $pic2 );
$arrlength = count($ChangeName);
for($x = 0; $x < $arrlength; $x++)
{
$config['upload_path'] = './uploads/';
$config['file_name'] = $ChangeName[$x];
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = 1909900;
$this->load->library('upload', $config);
$this->upload->do_upload($RealName[$x]);
echo $ChangeName[$x]; echo "<br>";
echo $RealName[$x]; echo "<br>";
}
}
Trying to upload multiple pictures. Code runs correctly but I'm facing some problems in saving all pictures. The problem is saving all pictures with same name (Name One).
try this one out. this code is I am using...
private function upload_files($path, $title, $files)
{
$config = array(
'upload_path' => $path,
'allowed_types' => 'jpg|gif|png',
'overwrite' => 1,
);
$this->load->library('upload', $config);
$images = array();
foreach ($files['name'] as $key => $image) {
$_FILES['images[]']['name']= $files['name'][$key];
$_FILES['images[]']['type']= $files['type'][$key];
$_FILES['images[]']['tmp_name']= $files['tmp_name'][$key];
$_FILES['images[]']['error']= $files['error'][$key];
$_FILES['images[]']['size']= $files['size'][$key];
$fileName = $title .'_'. $image;
$images[] = $fileName;
$config['file_name'] = $fileName;
$this->upload->initialize($config);
if ($this->upload->do_upload('images[]')) {
$this->upload->data();
} else {
return false;
}
}
return $images;
}

How to insert three images using codeigniter

This is my view file
<div class="form-group">
<label>Select Package Image(Large) </label>
<?= form_input(array('type'=>'file','id'=>'pkg_img','name'=>'pkg_img','class'=>'form-control','multiple'=>'')); ?>
</div>
<div class="form-group">
<label>Select Package Image(Mediam)</label>
<?= form_input(array('type'=>'file','id'=>'pkg_img_md','name'=>'pkg_img_md','class'=>'form-control','multiple'=>'')); ?>
</div>
<div class="form-group">
<label>Select Package Image(Small)</label>
<?= form_input(array('type'=>'file','id'=>'pkg_img_sm','name'=>'pkg_img_sm','class'=>'form-control','multiple'=>'')); ?>
</div>
Use Html helper class
$image_properties = array(
'src' => 'images/picture.jpg',
'alt' => 'Me, demonstrating how to eat 4 slices of pizza at one time',
'class' => 'post_images',
'width' => '200',
'height'=> '200',
'title' => 'That was quite a night',
'rel' => 'lightbox'
);
img($image_properties);
// <img src="http://url/index.php/images/picture.jpg" alt="Me, demonstrating how to eat 4 slices of pizza at one time" class="post_images" width="200" height="200" title="That was quite a night" rel="lightbox" />
Assuming that you just want to resize the same image differently (small, medium) sizes you can use the following code. If that is not what you want to accomplish than you can modify it to your liking:
class Test extends CI_Controller {
public function index() {
$this->load->helper('form');
echo form_open_multipart('/test/upload_image/');
echo form_upload('userfile');
echo form_submit(array('name' => 'add_file', 'value' => 'Add File'));
}
public function upload_image() {
if (!is_dir('./uploads/')) {
mkdir('./uploads/', 0755);
}
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|bmp';
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('userfile')) {
// handle this with flash messages & redirect
exit($this->upload->display_errors());
}
$image = $this->upload->data();
$this->load->library('image_lib');
$config = array(); // reset config array
$config['image_library'] = 'gd2';
$config['source_image'] = $image['full_path'];
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = FALSE;
$config['thumb_marker'] = '';
$config_sm['new_image'] = "./uploads/{$image['raw_name']}_sm{$image['file_ext']}";
$config_sm['width'] = 50;
$config_sm['height'] = 50;
$this->image_lib->initialize(array_merge($config, $config_sm));
$this->image_lib->resize();
$this->image_lib->clear();
$config_md['new_image'] = "./uploads/{$image['raw_name']}_md{$image['file_ext']}";
$config_md['width'] = 100;
$config_md['height'] = 100;
$this->image_lib->initialize(array_merge($config, $config_md));
$this->image_lib->resize();
$data = array(
'pkg_img_sm' => $config_sm['new_image'],
'pkg_img_md' => $config_md['new_image'],
'pkg_img' => $image['full_path'],
);
print_r($data);
//$this->db->insert('sometable', $data);
}
}
Will generate an array that looks like:
Array ( [pkg_img_sm] => ./uploads/936174c69e709b4c6d7fb840c4094eba_sm.jpg [pkg_img_md] => ./uploads/936174c69e709b4c6d7fb840c4094eba_md.jpg [pkg_img] => C:/xampp/htdocs/uploads/936174c69e709b4c6d7fb840c4094eba.jpg )

Upload and view multiple images in the same web page in codeigniter

This image shows what I need to do with codeigniter. I have one page with several div tags. I need to upload images and show them at the same place. But there should be 3 different images and 3 different file locations for these images to save. I tried number of ways. Please anyone with idea help me.
My controller
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Upload_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index(){
$this->load->view('file_view', array(
'error' => ' '
));
}
public function file_view()
{
$this->load->view('file_view', array(
'error' => ' '
));
}
public function do_upload()
{
$config = array(
'upload_path' => "./uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'overwrite' => TRUE,
'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload', $config);
if ($this->upload->do_upload()) {
$data = array(
'upload_data' => $this->upload->data()
);
$this->load->view('file_view', $data);
} else {
$error = array(
'error' => $this->upload->display_errors()
);
$this->load->view('file_view', $error);
}
}
public function do_upload2()
{
$config = array(
'upload_path' => "./uploads/index2/",
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'overwrite' => TRUE,
'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload', $config);
if ($this->upload->do_upload()) {
$data = array(
'upload_data' => $this->upload->data()
);
$this->load->view('file_view', $data);
} else {
$error = array(
'error1' => $this->upload->display_errors()
);
$this->load->view('file_view', $error);
}
}
}
?>
My View
<div id="1">
<?php echo form_open_multipart( 'upload_controller/do_upload');?>
<?php echo "<input type='file' name='userfile' size='20' />"; ?>
<?php echo "<input type='submit' name='submit' value='upload' /> ";?>
<?php echo "</form>"?>
</div>
<div id="2">
<h3>Your file was successfully uploaded!</h3>
<!-- Uploaded file specification will show up here -->
<ul>
<li>
</li>
<img alt="Your uploaded image" src="<?=base_url(). 'uploads/' . $upload_data['file_name'];?>">
</ul>
</div>
<div id="3">
<?php echo form_open_multipart( 'upload_controller/do_upload2');?>
<?php echo "<input type='file' name='userfile' size='20' />"; ?>
<?php echo "<input type='submit' name='submit' value='upload' /> ";?>
<?php echo "</form>"?>
</div>
<div id="4">
<h3>Your file was successfully uploaded!</h3>
<!-- Uploaded file specification will show up here -->
<ul>
<li>
</li>
<img alt="Your uploaded image" src="<?=base_url(). 'uploads/index2/' . $upload_data['file_name'];?>">
</ul>
<p>
<?php echo anchor('upload_controller/file_view', 'Upload Another File!'); ?>
</p>
</div>
Try This Code:
<form method="post" action="uploader/go" enctype="multipart/form-data">
<input type="file" name="image1" /><br />
<input type="file" name="image2" /><br />
<input type="file" name="image3" /><br />
<input type="file" name="image4" /><br />
<input type="file" name="image5" /><br />
<input type="submit" name="go" value="Upload!!!" />
</form>
Here Controller code:
class Uploader extends Controller {
function go() {
if(isset($_POST['go'])) {
/* Create the config for upload library */
/* (pretty self-explanatory) */
$config['upload_path'] = './assets/upload/'; /* NB! create this dir! */
$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
/* Load the upload library */
$this->load->library('upload', $config);
/* Create the config for image library */
/* (pretty self-explanatory) */
$configThumb = array();
$configThumb['image_library'] = 'gd2';
$configThumb['source_image'] = '';
$configThumb['create_thumb'] = TRUE;
$configThumb['maintain_ratio'] = TRUE;
/* Set the height and width or thumbs */
/* Do not worry - CI is pretty smart in resizing */
/* It will create the largest thumb that can fit in those dimensions */
/* Thumbs will be saved in same upload dir but with a _thumb suffix */
/* e.g. 'image.jpg' thumb would be called 'image_thumb.jpg' */
$configThumb['width'] = 140;
$configThumb['height'] = 210;
/* Load the image library */
$this->load->library('image_lib');
/* We have 5 files to upload
* If you want more - change the 6 below as needed
*/
for($i = 1; $i < 6; $i++) {
/* Handle the file upload */
$upload = $this->upload->do_upload('image'.$i);
/* File failed to upload - continue */
if($upload === FALSE) continue;
/* Get the data about the file */
$data = $this->upload->data();
$uploadedFiles[$i] = $data;
/* If the file is an image - create a thumbnail */
if($data['is_image'] == 1) {
$configThumb['source_image'] = $data['full_path'];
$this->image_lib->initialize($configThumb);
$this->image_lib->resize();
}
}
}
/* And display the form again */
$this->load->view('upload_form');
}
}

Content not showing in there correct module positions

I have a controller that shows modules for my each position
Array
(
[0] => Array
(
[layout_module_id] => 1
[layout_id] => 1
[module_id] => 1
[position] => column_left
[sort_order] => 1
)
[1] => Array
(
[layout_module_id] => 2
[layout_id] => 1
[module_id] => 2
[position] => column_left
[sort_order] => 2
)
)
Above currently I have only two modules set and the are in the position of column left.
Because the position views are out side of the foreach loop they are picking up that module even though not set for that position? As shown in image.
Question: How can I make sure that the module will only display in its set position view.
public function index() {
$layout_id = $this->getlayoutID($this->router->class);
$modules = $this->getlayoutsmodule($layout_id);
echo '<pre>';
print_r($modules);
echo "</pre>";
$data['modules'] = array();
foreach ($modules as $module) {
$this->load->library('module/question_help');
$data['modules'][] = $this->load->view('module/question_help', $this->question_help->set(), TRUE);
}
// Position Views
$data['column_left'] = $this->load->view('column_left', $data, TRUE);
$data['column_right'] = $this->load->view('column_right', $data, TRUE);
$data['content_top'] = $this->load->view('content_top', $data, TRUE);
$data['content_bottom'] = $this->load->view('content_bottom', $data, TRUE);
// Main view
$this->load->view('welcome_message', $data);
}
public function getlayoutsmodule($layout_id) {
$this->db->select('*');
$this->db->from('layouts_module');
$this->db->where('layout_id', $layout_id);
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
}
}
Each of the position views have the same foreach loop
<?php if ($modules) { ?>
<?php foreach ($modules as $module) { ?>
<?php echo $module;?>
<?php } ?>
<?php }?>
main view
<div class="container">
<div class="row">
<?php echo $column_left; ?>
<?php if ($column_left && $column_right) { ?>
<?php $class = 'col-sm-6'; ?>
<?php } elseif ($column_left || $column_right) { ?>
<?php $class = 'col-sm-9'; ?>
<?php } else { ?>
<?php $class = 'col-sm-12'; ?>
<?php } ?>
<div id="content" class="<?php echo $class; ?>">
<?php echo $content_top; ?>
<h1>Welcome to CodeIgniter!</h1>
<div id="body">
<p>The page you are looking at is being generated dynamically by CodeIgniter.</p>
<p>If you would like to edit this page you'll find it located at:</p>
<code>application/views/welcome_message.php</code>
<p>The corresponding controller for this page is found at:</p>
<code>application/controllers/Welcome.php</code>
<p>If you are exploring CodeIgniter for the very first time, you should start by reading the User Guide.</p>
</div>
<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds. <?php echo (ENVIRONMENT === 'development') ? 'CodeIgniter Version <strong>' . CI_VERSION . '</strong>' : '' ?></p>
<?php echo $content_bottom; ?>
</div>
<?php echo $column_right; ?></div>
</div>
Got it working I have had to use a foreach switch statement
<?php
class Welcome extends CI_Controller {
public function index() {
$layout_id = $this->getlayoutID($this->router->class);
$column_left = '';
$column_right = '';
$content_top = '';
$content_bottom = '';
$position = array('column_left', 'column_right', 'content_top', 'content_bottom');
foreach ($position as $key) {
switch ($key) {
case 'column_left':
$data['modules'] = array();
$layout_module_results = $this->getlayoutsmodule($layout_id, 'column_left');
if (!empty($layout_module_results)) {
foreach ($layout_module_results as $layout_module_result) {
$data['modules'][] = array(
'position' => $layout_module_result['position']
);
}
}
$column_left = $this->load->view('column_left', $data, TRUE);
break;
case 'column_right':
$data['modules'] = array();
$layout_module_results = $this->getlayoutsmodule($layout_id, 'column_right');
if (!empty($layout_module_results)) {
foreach ($layout_module_results as $layout_module_result) {
$data['modules'][] = array(
'position' => $layout_module_result['position']
);
}
}
$column_right = $this->load->view('column_right', $data, TRUE);
break;
case 'content_top':
$data['modules'] = array();
$layout_module_results = $this->getlayoutsmodule($layout_id, 'content_top');
if (!empty($layout_module_results)) {
foreach ($layout_module_results as $layout_module_result) {
$data['modules'][] = array(
'position' => $layout_module_result['position']
);
}
}
$content_top = $this->load->view('content_top', $data, TRUE);
break;
case 'content_bottom':
$data['modules'] = array();
$layout_module_results = $this->getlayoutsmodule($layout_id, 'content_bottom');
if (!empty($layout_module_results)) {
foreach ($layout_module_results as $layout_module_result) {
$data['modules'][] = array(
'position' => $layout_module_result['position']
);
}
}
$content_bottom = $this->load->view('content_bottom', $data, TRUE);
break;
}
}
$data['column_left'] = $column_left;
$data['column_right'] = $column_right;
$data['content_top'] = $content_top;
$data['content_bottom'] = $content_bottom;
$this->load->view('welcome_message', $data);
}
}

how to create form for multple image upload in codeigniter

i have created form for multiple image upload.but not working that form.only one image uploading..i want multiple images are to be upload to folder and save image name in database..
My View File
<html>
<head>
<title>Product Upload</title>
<style>
#container
{
width:750px;
margin:0 auto;
//border:5px solid #000000;
}
#container form input[type="text"] {
height:30px;
}
</style>
</head>
<body>
<div id="container" align="center">
<form name="product" action="<?php echo base_url;?>admin/login/upload" method="POST" enctype="multipart/form-data" class="form-horizontal">
<table>
<h3>Add New Product</h3>
<tr><td>Categories</td><td><select name="catid"><option>Categories</option>
<?php if(isset($category_details))
{foreach($category_details as $keys=>$values){ ?>
<option value="<?php echo $values['cat_id'];?>"><?php echo $values['cat_name'];?></option>
<?php }
}?>
</select></td></tr>
<tr>
<td>Product Name:</td><td><input type="text" name="pname"></td></tr>
<tr><td><input type="file" multiple="true" name="userfile" size="20" /></td></tr>
<tr><td><br>Product Image:</td><td><br><input type="file" name="pimage[]" id="pimage" multiple></td></tr>
<tr><td><br>Description:</td><td><br><textarea name="pdescription"></textarea></td></tr>
<tr><td><br>Price:</td><td><br><input type="text" name="price"></td></tr>
<tr><td colspan="2" align="center"><br><input type="submit" name="submit" value="ADD" class="btn btn-primary"></td></tr>
</table>
</form>
</div>
</body>
</html>
My Controller File
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* #see http://codeigniter.com/user_guide/general/urls.html
*/
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->model('admin/category_model');
$this->load->model('admin/loginauth_model');
$this->load->model('login_model');
$this->load->library('session');
$this->load->helper('url');
$this->load->helper('cookie');
$this->load->library('encrypt');
$this->load->library('image_CRUD');
}
public function index()
{
$this->load->view('admin/login');
}
public function loadproduct()
{
$category_details=$this->category_model->getCategoryDetails();
$outputdata["category_details"]=$category_details;
$this->load->view('admin/product',$outputdata);
}
public function loginAuth()
{
$this->form_validation->set_rules('email','Enter Email','required|trim|max_length[50]|xss_clean');
$this->form_validation->set_rules('password','Enter your Password','required|trim|max_length[50]|xss_clean');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('admin/login');
}
else
{
$username=$_POST['email'];
$password=$_POST['password'];
$user_details=$this->loginauth_model->logincheck($username,$password);
//print_r($checkauth);
if($user_details)
{
if($this->session->userdata('adminusername'))
{
$adminusername=$this->session->userdata('adminusername');
$outputdata['username']=$adminusername;
}
$category_details=$this->category_model->getCategoryDetails();
$outputdata['category_details']=$category_details;
$this->load->view('admin/dashboard',$outputdata);
}
}
}
public function category()
{
//$this->load->view('admin/category');
$this->form_validation->set_rules('cat_name','category name','required|trim|max_length[50]|xss_clean');
$this->form_validation->set_rules('cat_desc','category description','required|trim|max_length[50]|xss_clean');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('admin/category');
}
else
{
$addcategory=$this->category_model->addcategory($_POST);
if($addcategory)
{
//**************************pending
$category_details=$this->category_model->getCategoryDetails();
$outputdata['category_details']=$category_details;
//print_r($outputdata);
$this->load->view('admin/categorylist',$outputdata);
}
}
}
public function categorylist()
{
//echo image_url;
$category_details=$this->category_model->getCategoryDetails();
$outputdata['category_details']=$category_details;
$outputdata['image_url']=image_url;
$this->load->view('admin/categorylist',$outputdata);
$this->load->view('admin/category');
}
public function userdetails()
{
$user_details=$this->login_model->userdetails();
$outputdata['user_details']=$user_details;
$this->load->view('admin/userdetails',$outputdata);
}
public function upload()
{
$productname=$_POST["pname"];
$description=$_POST["pdescription"];
$price=$_POST["price"];
$catid=$_POST["catid"];
$name_array = array();
echo $count = count($_FILES['pimage']['size']);
foreach($_FILES as $key=>$value)
for($s=0; $s<=$count-1; $s++) {
$_FILES['pimage']['name']=$value['name'][$s];
$_FILES['pimage']['type'] = $value['type'][$s];
$_FILES['pimage']['tmp_name'] = $value['tmp_name'][$s];
$_FILES['pimage']['error'] = $value['error'][$s];
$_FILES['pimage']['size'] = $value['size'][$s];
$config['upload_path'] = FCPATH.'img/product_uploads/original/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|GIF|PNG|JPG|JPEG';
$config['max_size'] = '100';
$config['max_width'] = '150';
$config['max_height'] = '180';
$this->load->library('upload', $config);
$this->upload->do_upload();
$data = $this->upload->data();
$name_array[] = $data['file_name'];
//$products=$this->category_model->addproduct($catid,$productname,$description,$imagename,$imagesize,$price,$path);
}
$names= implode(',', $name_array);
/* $this->load->database();
$db_data = array('id'=> NULL,
'name'=> $names);
$this->db->insert('testtable',$db_data);
*/ print_r($names);
//echo FCPATH;
//$productname=$_POST["pname"];
// $description=$_POST["pdescription"];
// $price=$_POST["price"];
//$catid=$_POST["catid"];
// $path = FCPATH.'img/product_uploads/original/';
// $valid_formats = array("jpg", "png", "gif", "bmp","jpeg","PNG","JPG","JPEG","GIF","BMP","PNG");
// if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST") {
// $imagename = $_FILES['pimage']['name'];
// $imagesize = $_FILES['pimage']['size'];
// list($txt, $ext) = explode(".", $imagename);
//$products=$this->category_model->addproduct($catid,$productname,$description,$imagename,$imagesize,$price,$path);
//print_r($products);
// $tmp = $_FILES['pimage']['tmp_name'];
//if(move_uploaded_file($tmp, $path.$imagename)) {
// $product_details=$this->category_model->getProductDetails();
//print_r($product_details);
//if(isset($products)){
//echo "aa";
// $this->loadproduct();
//}
// }
//else
// {
//echo "Image Upload Failed.";
//}
}
public function logout()
{
$newdata = array(
'adminuser_id' =>'',
'adminusername' =>'',
'adminemail' => '',
'logged_in' => FALSE,
);
$this->session->unset_userdata($newdata);
$this->session->sess_destroy();
$this->index();
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
You can't just add a multiple attrinbute to <input type="file" />, that's not going to work.
You'll have to use a third-party module/plugin, like Uploadify, to get this functionality running.
You could also check this tutorial on how to integrate Uploadify in CodeIgniter (although I can't see anything CodeIgniterish in your code, but you still tagged the question as CI relevant)
a) Instead of writing this <input type="file" multiple="true" name="userfile" size="20" /> write <input type="file" name="userfile[]" size="20" />
b) Inside your controller you can do something like this:
$files = $_FILES;
$count = count($_FILES['userfile']['name']);
for($i=0; $i<$count; $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($config);
$this->upload->do_upload();
$image_data = $this->upload->data();
}
Use this to open your upload form :
echo form_open_multipart('admin/do_upload');
After put one or more :
<input type="file" name="name" size="20" />
And use a function like that :
public function do_upload() {
$config['upload_path'] = './assets/images/upload/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('admin/images', $error);
} else {
$data = array('upload_data' => $this->upload->data());
}
}

Resources