i make alert if image size > to max_size with javascript. but why always appeared alert "too big picture" when the image size under max_size. there was something I forgot?
my view :
<?php echo form_open_multipart('#', array('id' => 'form-produk'));?>
<input type="text" name="nama" class="form-control" id="nama" placeholder="Nama produk" required>
<input type="file" name="gambar" class="form-control" id="gambar" required>
<input type="file" name="gambar_tambah" class="form-control" id="gambar" required>
<button type="submit" style="background-color:#1c2d3f;" class="simpan_produk btn btn-primary">Simpan Produk</button>
<?php echo form_close();?>
my javascript :
$('.simpan_produk').click(function(){
var UrlToPass = $("#form-produk").serialize();
$.ajax({ // Send the credential values to another checker.php using Ajax in POST menthod
type : "POST",
data : UrlToPass,
url : baseURL + "trueaccon2194/produk/proses_tambah_produk",
beforeSend: function(){
$('.simpan_produk').prop('disabled', true);
$('.simpan_produk').html('sedang menyimpan...'); //Loading button text
},
success : function(write){
$('.simpan_produk').prop('disabled', false);
$('.simpan_produk').html('Simpan'); //reset button text to original text
if (write=="datasuccesswrite"){
alert('data is saving!');
window.location.href = "../";
}else if(write=="imageistoolarge"){
alert('too big picture!');
location.reload();
}
}
});
return false;
});
my controller :
function proses_tambah_produk(){
$config['upload_path'] = 'assets/img/produk';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = 300;
$config['overwrite'] = TRUE;
//$config['maintain_ratio'] = TRUE;
//$config['create_thumb'] = TRUE;
//$config['max_width'] = 75;
//$config['max_height'] = 50;
$this->load->library('upload', $config);
if($_FILES['gambar']['size'] > 300){
log_helper("produk", "Gagal Menambah produk baru");
echo"imageistoolarge";
//redirect('trueaccon2194/produk/tambah_produk');
}else if(!$this->upload->do_upload('gambar_tambah')){
log_helper("produk", "Gagal Menambah produk baru");
echo"imageistoolarge";
//redirect('trueaccon2194/produk/tambah_produk');
}else{
$result=array();
$files = $_FILES;
$count = count($_FILES['gambar_tambah']['name']);
for($i=0; $i<$count; $i++)
{
$_FILES['gambar_tambah']['name']= $files['gambar_tambah']['name'][$i];
$_FILES['gambar_tambah']['type']= $files['gambar_tambah']['type'][$i];
$_FILES['gambar_tambah']['tmp_name']= $files['gambar_tambah']['tmp_name'][$i];
$_FILES['gambar_tambah']['error']= $files['gambar_tambah']['error'][$i];
$_FILES['gambar_tambah']['size']= $files['gambar_tambah']['size'][$i];
$this->upload->do_upload('gambar_tambah');
$upload_data = $this->upload->data();
$name_array[] = $upload_data['file_name'];
$fileName = $upload_data['file_name'];
$images[] = $fileName;
$result[] = $files['gambar_tambah']['name'][$i];
}
$target = $this->input->post('nama');
$this->upload->do_upload('gambar');
$gambar = $_FILES['gambar']['name'];
$data = $this->input->post();
$data['id'] = $this->data['id'];
$this->produk_adm->add($data, $gambar, $result);
log_helper("produk", "Menambah Produk ".$target."");
echo "datasuccesswrite";
//redirect('trueaccon2194/produk');
}
}
In your view id must be unique for each element.So make id of second file id="gambar_tambah".
<?php echo form_open_multipart('#', array('id' => 'form-produk'));?>
<input type="text" name="nama" class="form-control" id="nama" placeholder="Nama produk" required>
<input type="file" name="gambar" class="form-control" id="gambar" required>
<input type="file" name="gambar_tambah" class="form-control" id="gambar_tambah" required>
<button type="submit" style="background-color:#1c2d3f;" class="simpan_produk btn btn-primary">Simpan Produk</button>
<?php echo form_close();?>
id should be unique for any element.
<input type="file" name="gambar" class="form-control" id="gambar" required>
<input type="file" name="gambar_tambah" class="form-control" id="gambar_tambah" required>
You are not checking size of both files uploaded.
if($_FILES['gambar']['size'] > 300){
log_helper("produk", "Gagal Menambah produk baru");
echo"imageistoolarge";
} else {
// upload files
}
if($_FILES['gambar_tambah']['size'] > 300){
log_helper("produk", "Gagal Menambah produk baru");
echo"imageistoolarge";
} else {
// upload files
}
Related
Hi friends in my code image upload in my folder but it's not saved in database am not knowledge about CI so help me for this
view code for upload image,
<form role="form" method="post" enctype="multipart/form-data">
<fieldset class="form-group">
<input type="hidden" name="txt_hidden" value="" class="form-control">
</fieldset>
<fieldset class="form-group">
<label class="control-label" for="formGroupExampleInput">Add Main Caregory</label>
<input type="text" value="<?php echo set_value('p_name'); ?>" placeholder="Main Category" name="p_name" class="form-control">
</fieldset>
<fieldset class="form-group">
<label class="control-label" for="formGroupExampleInput2">Order</label>
<input type="text" name="order_id" placeholder="Order Id" value="<?php echo set_value('order_id'); ?>" class="form-control" id="formGroupExampleInput2">
</fieldset>
<fieldset class="form-group">
<label class="control-label" for="formGroupExampleInput2">Status ( 0 active , 1 inactive)</label>
<input type="text" name="status" placeholder="Status" value="<?php echo set_value('status'); ?>" class="form-control" id="formGroupExampleInput2">
</fieldset>
<fieldset class="form-group">
<label class="control-label" for="formGroupExampleInput2">Image</label>
<input type="file" name="image" placeholder="Category Image" value="<?php echo set_value('image'); ?>" class="form-control" id="formGroupExampleInput2">
</fieldset>
<div class="form-group">
<button type="submit" class="btn btn-primary">Add Category</button>
</div>
</form>
in My controller
use this for add category with an image
function add_main_category()
{
$this->form_validation->set_rules('p_name', 'Main Category', 'required|is_unique[main_category.p_name]');
$this->form_validation->set_rules('order_id', 'Order Id');
$this->form_validation->set_rules('status', 'Status');
$this->form_validation->set_rules('image', 'Category Image', 'callback_cat_image_upload');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('master/add_main_category');
}
else
{
$p_name = strtolower($this->input->post('p_name'));
$order_id = strtolower($this->input->post('order_id'));
$status = strtolower($this->input->post('status'));
$image = strtolower($this->input->post('image'));
$data = array(
'p_name' => $p_name,
'order_id' => $order_id,
'status' => $status,
'image' => $image
);
$insert = $this->m->CategoryAdd($data);
if($insert){
$this->session->set_flashdata('success_msg','Category Created Successfully!!');
}
else{
$this->session->set_flashdata('error_msg', 'Failed to delete Category');
}
redirect('admin/main_cat');
}
}
and for image upload use this in controller
function cat_image_upload(){
if($_FILES['image']['size'] != 0){
$upload_dir = './uploads/';
if (!is_dir($upload_dir)) {
mkdir($upload_dir);
}
$config['upload_path'] = $upload_dir;
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['file_name'] = 'userimage_'.substr(md5(rand()),0,7);
$config['overwrite'] = false;
$config['max_size'] = '5120';
$this->load->library('upload', $config);
if (!$this->upload->do_upload('image')){
$this->form_validation->set_message('cat_image_upload', $this->upload->display_errors());
return false;
}
else{
$this->upload_data['file'] = $this->upload->data();
return true;
}
}
else{
$this->form_validation->set_message('cat_image_upload', "No file selected");
return false;
}
}
table main_category in column image
everything is working only no image name save in database!
help me with this.
Thanks in advance!
You can't store the image in the way you used as:
$image = strtolower($this->input->post('image'));
Get the image file name once it is uploaded. Here is your modified code:
if (!$this->upload->do_upload('image')){
$this->form_validation->set_message('cat_image_upload', $this->upload->display_errors());
return false;
}
else{
$this->upload_data['file'] = $this->upload->data();
$data = array('upload_data' => $this->upload->data());
$doc_file_name = $data['upload_data']['file_name']; // Use this file name to store in database
return true;
}
I’m working in form to upload image in project folder and store the other data into Database. when I try to submit this form data was submitted and image was not uploaded and one more the image was uploaded and not all of data was stored in database this is my code
model:
function update_news($data) {
extract($data);
$this->db->where('news_id', $news_id);
$this->db->update($table_name, array('title_en' => $title_en, 'title_ar' => $title_ar,'news_date' => $news_date,'image' => $image,'is_visible' => $is_visible,'main_news' => $main_news));
return true;
}
controller
public function update() {
$filename = $this->input->post('image');
$date = DateTime::createFromFormat("Y-m-d", $this->input->post('news_date'));
$data = array(
'table_name' => 'news', // pass the real table name
'news_id' => $this->input->post('news_id'),
'title_en' => $this->input->post('title_en'),
'title_ar' => $this->input->post('title_ar'),
'news_date' => $this->input->post('news_date'),
'image' => $date->format("Y") . '/' . $this->input->post('image'),
'is_visible' => $this->input->post('is_visible'),
'main_news' => $this->input->post('main_news')
);
$this->m_news_crud->update_news($data);
$folderName = 'assets/images/press-news/2015';
$config['upload_path'] = "$folderName";
if (!is_dir($folderName)) {
mkdir($folderName, 0777, TRUE);
}
$config['file_name'] = $this->input->post('image');
$config['overwrite'] = TRUE;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('userfile','image')) {
$error = array('error' => $this->upload->display_errors());
$data['error'] = $error;
$data['title_en'] = $this->input->post('title_en');
$data['title_ar'] = $this->input->post('title_ar');
$data['news_date'] = $this->input->post('news_date');
$data['image'] = base_url('assets/images/press-news/' . $date->format("Y")) . '/' . $this->input->post('image');
$data['is_visible'] = $this->input->post('is_visible');
$data['main_news'] = $this->input->post('main_news');
$data['news_id'] = $this->input->post('news_id');
$this->load->view('admin/news/d_admin_header');
$this->load->view('admin/news/vcrudedit_news', $data);
$this->load->view('admin/news/d_admin_footer');
} else {
$data = array('upload_data' => $this->upload->data());
redirect('c_news_crud/get_news_data', $data);
echo $filename;
}
}
view:
<div class="container">
<div class="col-md-9">
<?php echo form_open('C_news_crud/update', 'role="form" style="margin-top: 50px;"'); ?>
<div class="form-group">
<label for="title_en">Title Name English</label>
<input type="text" class="form-control" id="title_en" name="title_en" value="<?php echo $title_en ?>">
</div>
<div class="form-group">
<label for="title_ar">Title Name Arabic</label>
<input type="text" class="form-control" id="title_ar" name="title_ar" value="<?php echo $title_ar ?>">
</div>
<div class="form-group">
<div class="col-md-4 col-md-offset-4">
<label for="is_visible">is visible</label>
<?php
if ($is_visible == 1) {
$check_visible = 'checked="checked"';
} else {
$check_visible = ' ';
}
?>
<input role="checkbox" type="checkbox" id="is_visible" class="cbox" <?php echo $check_visible; ?> name="is_visible" value="<?php echo $is_visible; ?>">
</div>
<div class="col-md-4">
<label for="main_news">Main Arabic</label>
<?php
if ($main_news == 1) {
$check_main = 'checked="checked"';
} else {
$check_main = ' ';
}
?>
<input role="checkbox" type="checkbox" id="main_news" class="cbox" <?php echo $check_main; ?> name="main_news" value="<?php echo $main_news; ?>">
</div>
</div>
<div class="form-group">
<label for="news_date">News date</label>
<input type="date" class="form-control" id="news_date" name="news_date" value="<?php echo $news_date ?>">
</div>
<div class="form-group">
<label for="image">image</label>
<input type="file" class="form-control" id="image" name="image" value="<?php echo $image ?>">
</div>
<input type="hidden" name="news_id" value="<?php echo $news_id ?>" />
<input type="submit" name="save" class="btn btn-primary" value="Update">
<button type="button" onclick="location.href = '<?php echo site_url('C_news_crud/get_news_data') ?>'" class="btn btn-success">Back</button>
</form>
<?php echo form_close(); ?>
</div>
Make sure the path to the uploading folder is correct and it has write permission.
Make sure you are referring to the correct file input name:
$config['file_name'] = $_FILES['image']['name'];
set your uploading directory as ./assets/images/press-news/2015/
You need to display the error you get so that you know what is causing the error!
if ( ! $this->upload->do_upload()){
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
Codigniter's Upload Class
I am uploading images in codeigniter. my file is not getting uploaded. i dont know where i am doing wrong. please find in below code
Html:
<form method="post" enctype="multipart/form-data" action="<?php echo base_url(); ?>assign_user/addStartPopup">
<div class="col-lg-6">
<div class="form-group">
<label for="image">
<input type="radio" id="image" name="start_img" onclick="rightfilefunction(1);" checked> <label for="image"> <span style="top: -2px;position: relative;">Image upload</span></label>
<label for="video" class="green">
<input type="radio" id="video" name="start_img" onclick="rightfilefunction(2);">
<span style="top: -2px;position: relative;">Video</span>
</label>
</div>
<div class="form-group" id="imageId">
<input type="file" name="imageupload" class="form-control">
</div>
<div class="form-group" id="videoId" style="display:none">
<input type="text" name="video_upload" class="form-control">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary"> Save </button>
</div>
</div>
</form>
Model:
public function startPopup(){
$createdOn = date('Y-m-d H:i:s');
if($_FILES['imageupload']['size'] != 0)
{
$files = $_FILES;
$config = array();
$config['upload_path'] = "startpage/";
$config['allowed_types'] = "png|jpg|gif";
$config['max_size'] = "5000";
$this->load->library('upload',$config);
$this->upload->initialize($config);
if($this->upload->do_upload('imageupload')){
$imgdata = $this->upload->data();
$image_config=array();
$image_config["image_library"] = "gd2";
$image_config['overwrite'] = TRUE;
$image_config["source_image"] = $imgdata["full_path"];
$image_config['create_thumb'] = FALSE;
$image_config['maintain_ratio'] = TRUE;
$image_config['new_image'] = $imgdata["file_path"].$imgdata["file_name"];
$image_config['quality'] = "95%";
$image_config['width'] = 170;
$image_config['height'] = 170;
$this->load->library('image_lib',$image_config);
$this->image_lib->initialize($image_config);
$this->image_lib->resize();
$logo = 'startpage/'.$imgdata["file_name"];
$query = $this->db->query("INSERT INTO `startpop` (content_view, created_on) VALUES ('$logo', '$createdOn')");
if($query){
return 1;
}
}else
{
echo "not uploaded";
}
}else{
$prv = $this->input->post('video_upload');
$query = $this->db->query("INSERT INTO `startpop` (content_view, created_on) VALUES ('$prv', '$createdOn')");
if($query){
return 1;
}
}
}
Controller:
public function addStartPopup(){
$result = $this->model->startPopup();
if($result > 0){
$data['mgs'] = "Added Successfully ..!";
$this->load->view('admin/startPage',$data);
}
}
From this above code, i am getting the error "not uploaded". can any one tell me where i am doing wrong
I'm new to codeigniter, and I'm trying to make a form that updates a profile. It has worked before but now it doesn't anymore.
I changed something in the view and it stopped working. I changed it back to where it was but it doesn't work anymore. I spend half a day trying to make it work again but I failed.
Maybe I don't see what you guys can see. At least I hope so.
this is my view:
<?php echo form_open_multipart('Gids/do_upload');?>
<img width="200px" src="<?php echo base_url()."uploads/".$profile[0]['image']; ?>" alt=""/>
<label for="">Uploade new picture:</label><input type="file" name="userfile" size="20" value="128.jpg" >
<input style="display: none" id="image" name="image" type="text" value="<?php echo "profile_picture".$_SESSION['id'].".jpg"; ?>"/>
<label for="naam">Naam:</label><input id="naam" name="naam" type="text" value="<?php echo $profile[0]['naam']; ?>"/>
<label for="voornaam">Voornaam:</label><input id="voornaam" name="voornaam" type="text" value="<?php echo $profile[0]['voornaam']; ?>"/>
<label for="email">Gebruikersnaam:</label><input id="gebruikersnaam" name="gebruikersnaam" type="text" value="<?php echo $profile[0]['gebruikersnaam']; ?>"/>
<label for="email">Email:</label><input id="email" name="email" type="text" value="<?php echo $profile[0]['email']; ?>"/>
<label for="opleiding">Opleiding:</label><input id="opleiding" name="opleiding" type="text" value="<?php echo $profile[0]['opleiding']; ?>"/>
<label for="school">School:</label><input id="school" name="school" type="text" value="<?php echo $profile[0]['school']; ?>"/>
<label for="wachtwoord">Wachtwoord:</label><input id="wachtwoord" name="wachtwoord" type="text" />
<label for="typeAgain">Type Opnieuw:</label><input id="typeAgain" type="text" />
<label for="over">Over mezelf:</label><textarea name="over" id="over" cols="30" rows="10"><?php echo $profile[0]['over']?></textarea>
<input type='text' style="display: none" name='student_id' value="<?php echo $profile[0]['student_id']?>"/>
<button class="btn btn-default" id="changeprofile" type="submit">Wijzigingen opslaan</button>
</form>
As you can see there is also a image upload
this is my controller:
function do_upload()
{
$this->load->model("Gids_model",'',true);
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['file_name'] = 'profile_picture'.$_SESSION['id'].'.jpg';
$config['overwrite'] = 'TRUE';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error_upload = array('error' => $this->upload->display_errors());
redirect('Gids/datum', $error_upload);
}
else
{
$this->Gids_model->update_profile($this->input->post('student_id'), $this->input->post('voornaam'), $this->input->post('naam'), $this->input->post('email'), $this->input->post('wachtwoord'), $this->input->post('opleiding'), $this->input->post('school'), $this->input->post('over'), $this->input->post('image'), $this->input->post('gebruikersnaam'));
//$e = $this->input->post('student_id');
$data_upload = array('upload_data' => $this->upload->data());
redirect('Gids/datum', $data_upload);
}
}
And this is my model:
public function update_profile($id, $voornaam, $naam, $email, $wachtwoord, $opleiding, $school, $over, $image, $gebruikersnaam){
$data = array(
'student_id' => $id,
'voornaam' => $voornaam,
'naam' => $naam,
'email' => $email,
'wachtwoord' => $wachtwoord,
'opleiding' => $opleiding,
'school' => $school,
'over' => $over,
'image' => $image,
'gebruikersnaam' => $gebruikersnaam
);
$this->db->where('student_id', $id);
$this->db->update('tbl_student', $data);
}
Could you please help me out.
Also I don't know what the form_open_multipart('Gids/do_upload') is for, I got it from a tutorial to upload images with codeigniter.
Here is what i tried
public function do_upload() {
$this->load->model("Gids_model",'',true);
// load library or you can do it with autoloading
// feature of CI
$this->load->library('upload');
// make sure that the folder uploads is created
// at the root directory of the project
$config = array(
'upload_path' => './uploads',
'allowed_types' => 'jpg|jpeg|JPG|JPEG|png',
'max_size' => '1000',
'file_name' => 'profile_picture.jpg',
'overwrite' => true
);
// use initialize instead
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('userfile'))
{
$error_upload = array('error' => $this->upload->display_errors());
// you can check the errors here by using var_dump();
// just uncomment the the line below
// var_dump($this->upload->display_errors());die();
redirect('Gids/datum', $error_upload);
}
else
{
$this->Gids_model->update_profile($this->input->post('student_id'), $this->input->post('voornaam'), $this->input->post('naam'), $this->input->post('email'), $this->input->post('wachtwoord'), $this->input->post('opleiding'), $this->input->post('school'), $this->input->post('over'), $this->input->post('image'), $this->input->post('gebruikersnaam'));
//$e = $this->input->post('student_id');
$data_upload = array('upload_data' => $this->upload->data());
redirect('Gids/datum', $data_upload);
}
}
I tried this and hope it helps:
Controller :
public function upload_profile() {
$input = $this->input->post();
$config['upload_path'] = './uploads/profile_pics/'; //path were I save the uploaded profile pics
$config['allowed_types'] = 'gif|jpg|png'; // allowed types that is mention
//size of the picture by default
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['overwrite'] = true;
$this->load->library('upload', $config);
// display error if the picture is not on the config (sample bmp)
if ( ! $this->upload->do_upload())
{
$error = $this->upload->display_errors(); // display the errors
$data['upload_error'] = $error;
if($this->session->userdata('account_id') != null) { // if there is an account
$this->load->model('profile'); //model
$this->load->model('account'); //model
$data['user'] = $this->profile->get_profile($this->session->userdata('account_id')); //get_profile is a function in model
$data['account'] = $this->account->get_account($this->session->userdata('account_id')); //get_account is a function in model
$data['view'] = 'users/settings';
$data['title'] = "User's Settings";
$data['error'] = $error;
$this->load->view('masterpage', $data);
} else {
redirect(base_url('index.php/qablog/login'));
}
}
else
{
//if no error
$data = $this->upload->data();
$updateProfile = array(
'profile_pic' => $data['file_name']
);
$this->load->model('profile');
$this->profile->update_profile($this->session->userdata('account_id'), $updateProfile); // update the profile of the user
redirect(base_url('index.php/users/profile'));
}
}
Model get_profile():
public function get_profile($profile_id)
{
$this->db->select()->from('profile_tbl')->where('profile_id', $profile_id);
$query = $this->db->get();
return $query->first_row('array');
}
Model update_profile():
public function update_profile($profile_id, $data)
{
$this->db->where('profile_id', $profile_id);
$this->db->update('profile_tbl', $data);
return $this->db->affected_rows();
}
Model get_account():
public function get_account($account_id)
{
$this->db->select()->from('account_tbl');
$this->db->where('account_id', $account_id);
$query = $this->db->get();
return $query->result_array();
}
View :
//if there is an error
<?php
if ($error == 3) {?>
<div class="alert alert-success">
×
<strong>Success!</strong> Account or Profile Changed Successfully.
</div>
<?php } else if ($error == 1) { ?>
<div class="alert alert-warning">
×
<strong>Warning!</strong>Password Entered is Incorrect!.
</div>
<?php }else if ($error == 2) { ?>
<div class="alert alert-warning">
×
<strong>Warning!</strong>New Password and Confirm Password!.
</div>
<?php }?>
// default.png if haven't uploaded profile picture
<?php $profilePic = "default.png"; ?>
// if already uploaded profile picture it will display
<?php if($user['profile_pic'] != null) { ?>
<?php $profilePic = $user['profile_pic']; ?>
<?php } ?>
// if there is an error in uploading
<?php if(isset($upload_error)) { ?>
<div class="col-lg-12">
<div class="alert alert-danger alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
Uploading profile image could not be completed. <?php print_r($upload_error); ?>
</div>
</div>
<?php } ?>
<div class="col-lg-12">
//display the profile picture
<img src="<?php echo base_url('uploads/profile_pics/'.$profilePic); ?>" width="100" />
// call the controller upload_profile
<?php echo form_open_multipart(base_url('index.php/users/upload_profile'));?>
<input type="file" name="userfile" id="userfile" size="20" style="display:none;" />
<label for="userfile" class="btn btn-info btn-sm">Choose Image</label>
<input type="submit" class="btn btn-xs" value="edit profile" />
</form>
<h3 class="text-info"><?php echo $user['fname'].' '.$user['lname']; ?></h3>
</div>
Just follow this, and if you have problem just tell me. :)
in my CodeIgniter project I'm uploading files during the project creation. Here's the uploading function:
public function addProcess()
{
echo 'test';
if($this->input->server('REQUEST_METHOD') === 'POST')
{
$this->form_validation->set_rules('album', 'Album', 'trim|required|callback_check_select', 'xss_clean');
if ($this->form_validation->run() === FALSE)
{
$data['albums'] = $this->album_model->albumList();
$this->adminTheme('admin/addSong',$data);
}
else
{
$error = 0;
$this->load->library('upload');
$total_count_of_files = count($_FILES['userfile']['name']);
$album = $this->input->post('album');
$songs = $this->input->post('song');
$artist = $this->input->post('artist');
for($i=0; $i< $total_count_of_files; $i++)
{
if(!$songs[$i])
$songs[$i] = "Track-".($i+1);
if(!$artist[$i])
$artist[$i] = " ";
$_FILES['file']['name'] = $_FILES['userfile']['name'][$i];
$_FILES['file']['type'] = $_FILES['userfile']['type'][$i];
$_FILES['file']['tmp_name'] = $_FILES['userfile']['tmp_name'][$i];
$_FILES['file']['error'] = $_FILES['userfile']['error'][$i];
$_FILES['file']['size'] = $_FILES['userfile']['size'][$i];
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'jpg|jpeg|gif|png|mp3';
$config['max_size'] = '0';
$config['overwrite'] = FALSE;
$config['encrypt_name'] = TRUE;
$this->upload->initialize($config);
if($this->upload->do_upload('file'))
{
$msg = $this->upload->data();
$name = $msg['file_name'];
$error += 0;
$this->song_model->add($songs[$i], $artist[$i], $name, $album);
}else{
$errorStr[] = "$songs[$i] Upload Faild";
$error += 1;
}
}
//$this->album_model->add();
if($error == 0)
{
//$this->msg('admin/song','Successfully added..Redirecting now...');
}
else
{
$this->msg('admin/song','Successfully added..Redirecting now...');
}
}
}
else
{
redirect('admin/album');
}
}
another function for doupload
public function do_upload()
{
$upload_path_url = base_url().'uploads/';
$config['upload_path'] = FCPATH.'uploads/';
$config['allowed_types'] = 'jpg|jpeg|gif|png|mp3';
$config['max_size'] = '30000';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload()) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload', $error);
} else {
$data = $this->upload->data();
$info->name = $data['file_name'];
$info->size = $data['file_size'];
$info->type = $data['file_type'];
$info->url = $upload_path_url .$data['file_name'];
$info->thumbnail_url = $upload_path_url .$data['file_name'];
$info->delete_url = base_url().'upload/deleteImage/'.$data['file_name'];
$info->delete_type = 'DELETE';
if (IS_AJAX) {
echo json_encode(array($info));
} else {
$file_data['upload_data'] = $this->upload->data();
$this->load->view('admin/upload_success', $file_data);
}
}
}
my form code
<div id="container">
<h2>Song Name - Add New</h2>
<div class="error">
<?php echo validation_errors(); ?>
</div>
<?php $attributes = array('id' => 'myform'); ?>
<?php echo form_open_multipart('admin/song/addProcess',$attributes); ?>
<div class="line">
<label style="width: 160px;">Album:</label>
<select class="category" name="album" style="width: 280px;">
<option value="select">Select Album</option>
<?php foreach($albums as $album): ?>
<option value="<?php echo $album['id']; ?>">
<?php echo $album['root_name']." --> ".$album['category_name']." --> ".$album['album_name']; ?></option>
<?php endforeach ?>
</select>
<input class="form-submit" style="margin-top: 6px;" type="button" id="more_fields" onclick="add_fields();" value="Add More" />
</div>
<!--end line-->
<div id="nameFields">
<div class="line">
<input type="text" name="song[]" class="input1" style="width: 180px;" placeholder="Song Name" />
<input type="text" name="artist[]" class="input1" style="width: 180px;" placeholder="Artist Name" />
<input type="file" name="userfile[]" class="category" style="width:80px;" />
</div>
<!--end line-->
</div>
<input type="submit" value="Save" class="form-submit" style="margin-right: 180px;" />
</form>
</div>
<!--end container-->
<script type="text/javascript">
function add_fields() {
document.getElementById('nameFields').innerHTML += "<div class='line'><input type='text' name='song[]' class='input1' style='width: 180px;' placeholder='Song Name' /><input type='text' name='artist[]' class='input1' style='width: 180px;' placeholder='Artist Name' /><input type='file' name='userfile[]' class='category' style='width:80px;' /></div>";
}
</script>
However this does not do anything. The files are not being uploaded. Even an empty directory is not being created. Could anybody help me to find the error?
Thanks.
In your addProcess code, remove below code
$_FILES['file']['name'] = $_FILES['userfile']['name'][$i];
$_FILES['file']['type'] = $_FILES['userfile']['type'][$i];
$_FILES['file']['tmp_name'] = $_FILES['userfile']['tmp_name'][$i];
$_FILES['file']['error'] = $_FILES['userfile']['error'][$i];
$_FILES['file']['size'] = $_FILES['userfile']['size'][$i];
and try
foreach ($_FILES as $fieldname => $fileObject)
{
if (!empty($fileObject['name']))
{
$this->upload->initialize($config);
if (!$this->upload->do_upload($fieldname))
{
$errors[] = $this->upload->display_errors();
}
else
{
//success code
}
}
}