This tutorial will show you how to upload and play video using Codeigniter. The uploaded video may or may not start playing automatically. CodeIgniter’s File Uploading Class permits files to be uploaded. You can set various preferences, restricting the type and size of the files.
database
id Primary int(10)
name varchar(222)
video text
type varchar(22)
date datetime
View Page
<div class="page-title-box">
<div class="panel-title">
<h4>Upload Videos</h4>
</div>
</div>
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-body">
<div class="panel-body">
<div class="border_preview">
<?php echo form_open_multipart("backend/cms/service/upload_video") ?>
<div class="form-group row">
<label for="headline_en" class="col-sm-2 col-form-label">Name : <i class="text-danger">*</i></label>
<div class="col-sm-8">
<input name="name" value="" class="form-control" placeholder="Name" type="text" id="name">
</div>
</div>
<div class="form-group row">
<label for="headline_en" class="col-sm-2 col-form-label">Upload Video : <i class="text-danger">*</i></label>
<div class="col-sm-8">
<input name="video" class="form-control" placeholder="Upload Video" type="file" id="video">
Allowed Formats : <span style="color: red;"> .MP4 |.AVI |.MKV (Max 5MB)</span>
</div>
</div>
<div class="form-group row">
<label for="type" class="col-sm-2 col-form-label">Type (Free/Paid) *</label>
<div class="col-sm-8">
<select name="type" id="type" class="form-control">
<option value="">Select Type </option>
<option value="free">Free</option>
<option value="paid">Paid</option>
</select>
</div>
</div>
<div class="row">
<div class="col-sm-9 col-sm-offset-2">
<!-- <a href="<?php echo base_url('admin'); ?>" class="btn btn-primary w-md m-b-5"><?php echo display("cancel") ?></a> -->
<button type="submit" class="btn btn-success w-md m-b-5">Upload</button>
</div>
</div>
<?php echo form_close() ?>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="panel-heading">
<div class="panel-title">
<h4><?php echo (!empty($title)?$title:null) ?> List</h4>
</div>
</div>
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-body">
<div class="table-responsive">
<table id="datatable" class="table table-bordered dt-responsive nowrap" style="border-collapse: collapse; border-spacing: 0; width: 100%;">
<thead>
<tr>
<th><?php echo display('sl_no') ?></th>
<th>Name</th>
<th>Video</th>
<th>Type</th>
<th>Date</th>
<th><?php echo display('Action') ?></th>
</tr>
</thead>
<tbody>
<?php if (!empty($video)) ?>
<?php $sl = 1; ?>
<?php foreach ($video as $value) { ?>
<tr>
<td><?php echo $sl++; ?></td>
<td><?php echo $value->name; ?></td>
<td> <a href="<?php echo base_url().$value->video ?>" target="blank">Video Link</a></td>
<td><?php echo $value->type; ?></td>
<td><?php echo $value->date; ?></td>
<td>
<a href="<?php echo base_url("backend/cms/service/delete_upload_video/$value->id") ?>" onclick="return confirm('<?php echo display("are_you_sure") ?>')" class="btn btn-danger btn-sm" data-toggle="tooltip" data-placement="right" title="Delete "><i class="ti-trash" aria-hidden="true"></i></a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php echo $links; ?>
</div>
</div>
</div>
</div>(code-box)
Controller
// ----------------------- video ---------------------------------//
public function upload_video()
{
$data['title'] = 'Video';
$data['video'] = $this->article_model->readUploadVideo();
$this->form_validation->set_rules('name', "Name", "required|max_length[255]");
$this->form_validation->set_rules('type', "Type", "required|max_length[255]");
if (empty($_FILES['video']['name']))
{
$this->form_validation->set_rules('video','Upload Videos','required');
}
// ---------------------------UPLOAD IMAGE-----------------------------
$config = [
'upload_path' => 'upload/videos/',
'allowed_types' => 'mp4|avi|mkv|MP4|AVI|MKV',
'overwrite' => false,
'maintain_ratio' => true,
'encrypt_name' => true,
'remove_spaces' => true,
'file_ext_tolower' => true
];
$this->load->library('upload', $config);
if ($this->upload->do_upload('video')) {
$data = $this->upload->data();
$image = $config['upload_path'].$data['file_name'];
}
else{
if(!empty($_FILES['banner_image']['name'])){
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata('exception',$this->upload->display_errors());
}
// redirect('customer/service/security');
}
// print_r($userdata);die();
// ---------------------------UPLOAD IMAGE END-----------------------------
$userdata = array(
'name' => $this->input->post('name'),
'type' => $this->input->post('type'),
'date' => date("Y-m-d h:i:s"),
);
// print_r($userdata);die();
$userdata['video']=(!empty($image)?$image:$this->input->post('video'));
if ($this->form_validation->run())
{
if ($this->article_model->createUplaodVideo($userdata)) {
$this->session->set_flashdata('message', 'Video added successfully!');
redirect("backend/cms/service/upload_video");
} else {
$this->session->set_flashdata('exception', display('please_try_again'));
redirect("backend/cms/service/upload_video");
}
}
$data['content'] = $this->load->view("backend/service/upload_video", $data, true);
$this->load->view("backend/layout/main_wrapper", $data);
}
public function delete_upload_video($video_id = null)
{
if ($this->article_model->deleteUploadVideo($video_id)) {
$this->session->set_flashdata('message',"Video deleted successfully!");
} else {
$this->session->set_flashdata('exception', display('please_try_again'));
}
redirect("backend/cms/service/upload_video");
}
// ----------------------- video End ---------------------------------// (code-box)
Model
//--------------------------Uplaod Video--------------------------------------//
public function createUplaodVideo($data = array())
{
return $this->db->insert('upload_video', $data);
}
public function readUploadVideo()
{
return $this->db->select("*")
->from('upload_video')
->order_by('id', 'desc')
// ->where('status','yes')
->get()
->result();
}
public function deleteUploadVideo($video_id = null)
{
return $this->db->where('id', $video_id)
->delete("upload_video");
}(code-box)