I have a problem in Codeigniter when i have two upload forms on the same view/site. It seems Codeigniter recognizes only submit from one form. Even if i change the input name in the form, problem still exists.
Below is my view and controler:
View:
<?php echo form_open_multipart('admin/upload/do_upload_ac');?>
<input type="file" name="actives" />
<br /><br />
<input class="btn btn-primary" type="submit" value="Add Active Emails" />
</form>
<?php echo form_open_multipart('admin/upload/do_upload_un');?>
<input type="file" name="unactives" />
<br /><br />
<input class="btn btn-primary" type="submit" value="Add Active Emails" />
</form>
And the controler:
public function do_upload_ac()
{
header('Cache-Control: must-revalidate');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'csv';
$config['max_size'] = 0;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('actives'))
{
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata('error', 'Csv Data Import failed!! Please check the file size or file type!');
//$this->load->view('admin/muc', $error);
redirect(base_url() . 'index.php/admin/muc');
}
else
{
$data = array('upload_data' => $this->upload->data());
$filename_ac = $this->upload->data('full_path');
$this->Upload_model->insert_csv_ac($filename_ac);
$this->Upload_model->clean_temp_ac();
$this->Upload_model->truncate_temp_ac();
$this->session->set_flashdata('success', 'Csv Data Imported Succesfully!');
//$this->load->view('success', $data);
//unlink($filename_ac);
redirect(base_url() . 'index.php/admin/muc');
}
}
public function do_upload_un()
{
header('Cache-Control: must-revalidate');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'csv';
$config['max_size'] = 0;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('unactives'))
{
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata('error', 'Csv Data Import failed!! Please check the file size or file type!');
//$this->load->view('admin/muc', $error);
redirect(base_url() . 'index.php/admin/muc');
}
else
{
$data = array('upload_data' => $this->upload->data());
$filename_ac = $this->upload->data('full_path');
$this->Upload_model->insert_csv_un($filename_un);
$this->Upload_model->clean_temp_un();
$this->Upload_model->truncate_temp_un();
$this->session->set_flashdata('success', 'Csv Data Imported Succesfully!');
//$this->load->view('success', $data);
unlink($filename_un);
redirect(base_url() . 'index.php/admin/muc');
}
}
I was searching here for similar problem, but i just found uploading multiple files at once. Any ideas? And thank you all.
Alright, to give you an understanding of multiple submit, I have rendered a sample view for you. I have created following view
With this code
<div class="container">
<div class="col-md-3" style="box-shadow: 2px 2px 2px grey">
<form action="<?php echo base_url().'admin/upload/do_upload_ac'?>" method="post" id="form1">
<div class="form-group">
<label > Upload 1</label>
<input type="file" name="file1" >
</div>
<div class="form-group">
<button type="button" class="btn btn-danger" onclick="submitOne()">Upload Image1</button>
</div>
</form>
</div>
<div class="col-md-3" style="box-shadow: 2px 2px 2px grey">
<form action="<?php echo base_url().'admin/upload/do_upload_un'?>" method="post" id="form2">
<div class="form-group">
<label > Upload 2</label>
<input type="file" name="file1" >
</div>
<div class="form-group">
<button type="button" class="btn btn-danger" onclick="submitTwo()">Upload Image2</button>
</div>
</form>
</div>
</div>
And this Script
<script>
function submitOne()
{
$('#form1').submit();
}
function submitTwo()
{
$('#form2').submit();
}
</script>
I press first button I get
Similarly If i press second button it shows me form2 will be submitted. What you do once the form is submitted is all in the URL where you are submitting the form.
Your code is working with no update required. But you should change html form close with codeigniter's form_close() or user html form tag to start form.
Related
My Controller :
$foto = $_FILES['foto']['name'];
$doc = $_FILES['doc']['name'];
$ambextFoto = explode(".",$foto);
$ambextDOc = explode(".",$doc);
$ekstensiFoto = end($ambextFoto);
$ekstensiDoc = end($ambextDoc);
$nama_baru_foto = 'foto-'.date('YmdHis');
$nama_baru_doc = 'doc-'.date('YmdHis');
$nama_file_foto = $nama_baru_foto.".".$ekstensiFoto;
$nama_file_doc = $nama_baru_doc.".".$ekstensiDoc;
$config['upload_path'] = './assets/userfiles/usulan/';
$config['allowed_types'] = 'pdf';
$config['file_name'] = $nama_file_foto;
$config['file_name'] = $nama_file_doc;
$this->upload->initialize($config);
if($this->upload->do_upload('foto') AND $this->upload->do_upload('doc')){
$data = array(
'member_id' => $this->session->userdata('member_id'),
'foto' => $foto,
'doc' => $doc
);
}else{
$this->session->set_flashdata('error','Upload File Failed');
redirect(site_url('data/usulan/add'));
}
My View :
<form action="<?php echo site_url('data/usulan/insert') ?>" method="post" enctype="multipart/form-data">
<div class="form-group">
<input type="file" class="form-control" name="foto" accept="application/pdf">
</div>
<div class="form-group">
<input type="file" class="form-control" name="doc" accept="application/pdf">
</div>
<div class="card-footer text-right">
<button type="submit" class="btn btn-primary ml-auto">Submit</button>
</div>
</form>
The files was uploaded but the name file upload only in variable $name_file_doc. All file was uploaded but only the name is of the name $name_file_doc.
Please help me
Use this:
if(isset($_FILES['foto']['name'])){
//write only foto file upload code here.
}
if(isset($_FILES['doc']['name'])){
//write only doc file upload code here.
}
I know my question isn't new but I don't know why it is not working.
In my controller, If is false and my
image doesn't upload and the data of image don't save in database and else is execute.
<form name="f" enctype="multipart/form-data" method="post" action="<?php echo base_url();?>index.php/aparteman/save" role="form">
<!--<input type="text" name="gh" id="gh"></input>-->
<input type="hidden" name="g" id="g" value=<?php echo $insert_id; ?>></input>
<div class="col-md-1"></div>
<div class="col-md-4">
<div class="input-group">
<span class="input-group-btn">
<span class="btn btn-primary" onclick="$(this).parent().find('input[type=file]').click();">
<span class="glyphicon-class glyphicon glyphicon-folder-open"></span> Browse</span>
<input onchange="$(this).parent().parent().find('.form-control').html($(this).val().split(/[\\|/]/).pop());" style="display: none; width:50%;" type="file" name="Name1" id="Name1"></input>
</span>
</form>
mycontroller
public function save()
{
$config['upload_path']='/upload/';
$config['allowed_types']= 'gif|jpg|png|jpeg';
$config['max_size']= 2000;
$config['max_width']= 1024;
$config['max_height']= 768;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ($this->upload->do_upload('Name1'))
{
$data = array('upload_data' => $this->upload->data());
$my_data['photo']=$data['upload_data']['file_name'];
$my_data = array(
'FIDKhane' => $this->input->post('g')
);
$this->load->model('apartemanmodel');
$this->apartemanmodel->insert_images($my_data);
}
else {
echo "...";
}
my model
function insert_images($my_data)
{
$this->db->set('Name1',$my_data['photo']);
$this->db->set('FIDKhane',$my_data['FIDKhane']);
$this->db->insert('imagekhane');
}
Thanks for your helping.
Looks like you have overwrite your variable $my_data. Try to use this code instead:
$my_data = array(
'photo' => $data['upload_data']['file_name'],
'FIDKhane' => $this->input->post('g')
);
I tried many ways to upload Videos through my website. I don't know why, I can save images, text files etc. but it is not possible to save videos into the folder and database.
Here is my view.
<form action="<?php echo base_url();?>galleries/add_vdo" method="post" enctype="multipart/form-data">
<div class="input-group">
<input type="text" class="form-control" placeholder="Enter Title" name="vdo_title" id="vdo_title">
<textarea class="form-control" placeholder="About Video" name="vdo_desc" id="vdo_desc"></textarea>
<input class="" name="ur_vdo" id="ur_vdo" type="file">
<input class="btn btn-info" type="submit" name="submit" value="Add Now"/>
<!--<button class="btn btn-info btn-lg" id="submit" type="button">Invite</button>-->
</div>
</form>
controller:
function add_vdo()
{
$data['ur_vdo'] = $this->gallery_model->add_new_vdos();
redirect('galleries/vdo_gal','refresh');
}
Model :
function add_new_vdos()
{
$this->load->helper('date');
date_default_timezone_set('Asia/Calcutta');
$config['upload_path'] = './uploads/myvideos/';
$config['allowed_types'] = 'mp4|avi|flv|wmv|';
$this->load->library('upload', $config);
$this->upload->initialize($config);
$field_name ="ur_vdo";
$rgpic="0";
if ( !$this->upload->do_upload($field_name))
{
$error = $this->upload->display_errors();
$data['error']=$error;
}
else
{
$data = $this->upload->data();
$regpicname=$data['file_name'];
$rgpic="1";
}
if($rgpic!=0)
{
$dts = array(
'familypost_caption'=>$this->input->post('vdo_title'),
'familypost_desc'=>$this->input->post('vdo_title'),
'user_id' =>$uid,
'familypost_vdo'=>$regpicname,
'familypost_status'=>'A',
'familypost_date'=>now()
);
}
else
{
$dts = array(
'familypost_caption'=>$this->input->post('vdo_title'),
'familypost_desc'=>$this->input->post('vdo_title'),
'user_id' =>$uid,
'familypost_status'=>'A',
'familypost_date'=>now()
);
}
$vdqr = $this->db->insert('roottildb_familyposts',$dts);
}
I don't know why, video upload fails without showing any error. How can I solve this issue?
I have a registration form in which user can upload multiple files. He can upload ten files once and one by one each. Now what I want is that to create the uniqueId before calling the upload function and pass it to the upload function for the mkdir().
Right now I am creating the unique Id in the upload function but don't want this actually.
<form target="upload_target" id="fileupload" method="post" action="<?php echo site_url('upload_file/upload_it'); ?>" enctype="multipart/form-data">
<div class="fileUpload btn btn-warning">
<span>Browse</span>
<input id="uploadBtn" type="file" multiple="multiple" name="files[]" class="upload" />
</div>
<input id="uploadFile" style="width: 160px; margin-top: 30px;float: left;height: 35px;" placeholder="Choose File" disabled="disabled" />
<button id="btnupload" style="padding: 4.5px; float:left;margin-top: 30px;border-radius: 0px;" disabled="disabled" type="submit" class="btn btn-primary btn-lg"><span class="glyphicon glyphicon-upload"></span></button>
</form>
function upload_it()
{
$this->load->helper('form');
$uniqueID = uniqid();
mkdir("application/uploads/".$uniqueID);
$config['upload_path'] = 'application/uploads/'.$uniqueID;
$config['allowed_types'] = '*';
$this->load->library('upload', $config);
$this->upload->initialize($config);
$this->upload->set_allowed_types('*');
$data['upload_data'] = '';
if (!$this->upload->do_multi_upload('files')) {
$data = array('msg' => $this->upload->display_errors());
}
else
{
$data['upload_data'] = $this->upload->get_multi_upload_data();
}
$this->load->vars($data);
$this->load->view('upload');
}
$uniqueID = uniqid();
mkdir("application/uploads/".$uniqueID);
function upload_it($upload_path)
{
$this->load->helper('form');
$config['upload_path'] = $upload_path;
$config['allowed_types'] = '*';
$this->load->library('upload', $config);
$this->upload->initialize($config);
$this->upload->set_allowed_types('*');
$data['upload_data'] = '';
if (!$this->upload->do_multi_upload('files')) {
$data = array('msg' => $this->upload->display_errors());
}
else
{
$data['upload_data'] = $this->upload->get_multi_upload_data();
}
$this->load->vars($data);
$this->load->view('upload');
}
Kindly have a visit on following link: Demo link for problem
You can view that form is not showing submitted values and not uploading file.
Following is form code :
<form id="frm" name="frm" method="post" enctype="multipart/form-data">
<fieldset class="fieldset2">
<legend class="legend2">Add Bid Type</legend>
<div class="form_fields">
<p>
<label for="subject" class="label2">Bid Type:</label>
<input type="input" id="type" name="type" class="textfield2" />
</p>
<p>
<label for="subject" class="label2">Bid Type Code:</label>
<input type="input" id="code" name="code" class="textfield2" />
</p>
<p>
<label for="description" class="label2">Bid Type Description:</label>
<textarea id="description" name="description" class="textarea2"></textarea>
</p>
<p>
<label for="userfile" class="label2">Icon:</label>
<input type="file" id="userfile" name="userfile" class="input_file" />
</p>
</div>
</fieldset>
<fieldset class="none">
<p>
<input type="submit" id="btnsubmit" name="btnsubmit" class="btn" value="Add" />
<input type="reset" id="btnreset" name="btnreset" class="btn" value="Reset" />
</p>
</fieldset>
</form>`
And following is controller code :
function create() {
$data = '';
echo '<pre>';
echo 'below is Post Fields Data:<br>';
print_r($this->input->post());
echo '<br>Post Fields Data Ends: ';
echo '</pre>';
$this->form_validation->set_rules('type', 'Bid Type', 'trim|required|xss_clean');
$this->form_validation->set_rules('code', 'Bid Type Code', 'trim|xss_clean');
$this->form_validation->set_rules('description', 'Bid Type description', 'trim|xss_clean');
$data['errors'] = array();
if ($this->form_validation->run()) {
$data = array(
'code' => $this->form_validation->set_value('code'),
'type' => $this->form_validation->set_value('type'),
'description' => $this->form_validation->set_value('description'),
);
if (!is_null($this->bid_types->create_bid_type($data))){
$data['errors']['success'] = 'Record Successfully Added!';
} else {
$errors = $this->bid_types->get_error_message();
foreach ($errors as $k => $v) $data['errors'][$k] = $this->lang->line($v);
}
$config['upload_path'] = base_url().'resource/images/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())
{
$data['errors']['upload'] = 'Not Uploaded';
//$data['errors']['upload'] = $this->upload->display_errors();
//$error = array('error' => $this->upload->display_errors());
//$this->load->view('upload_form', $error);
}
else
{
$data['errors']['upload'] = 'Yes Uploaded';
//$data['errors']['upload'] = $this->upload->data();
//$data = array('upload_data' => $this->upload->data());
//$this->load->view('upload_success', $data);
}
echo '<pre>';
print_r($this->upload->data());
echo '</pre>';
} /* END OF FORM VALIDATRION RUN */
if(!$this->input->is_ajax_request() && !$this->input->get_post('is_ajax'))
{
$this->load->view('bend/bid_type/create', $data);
}
} /* END OF FUNCTION CREATE */
Can some one guide me what and where Iam doing wrong and how it can be rectrified.
File data appears in $_FILE not in $_POST.
I think this line is your problem:
$config['upload_path'] = base_url().'resource/images/uploads/';
This $config['upload_path'] should point to a file system path, not a URL.
Try something more like:
$config['upload_path'] = realpath(APPPATH . '../resource/images/uploads/');
which will start from your application folder, up one folder then into resource/images/uploads
change it accordingly if its somewhere else.
p.s. Also check your write permissions.
put $this->output->enable_profiler(TRUE) on the first line of create() function, it will show you very usefull information $_POST global to debug your code like
Charles shows that the request from the form submit does contain the post data.
Does anything else happen before the create() function that could wipe the POST data?
I would add an action attribute to the form using echo form_open('/phpdemo/b1/admin/bid_type/create') as described in the user manual.
you can not use
$this->form_validation->set_rules('code', 'Bid Type Code', 'trim|xss_clean');
in multipart , so you must create your own function for validation and use callbacks