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?
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.
}
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 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.
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 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');
}