Upload image & video on codeigniter - php

I have some issue when I insert image and video on database, I am using codeigniter for program.
When I insert both a video and image, my code does not work, but if I upload only one of image/video it works fine.
How can I solve this issue to insert video and image?
My controller code:
private function _do_upload()
{
$config['upload_path'] = './assets/img/sni';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 3000; //set max size allowed in Kilobyte
$config['max_width'] = 1000; // set max width image allowed
$config['max_height'] = 1000; // set max height allowed
$config['file_name'] = round(microtime(true) * 1000); //just milisecond timestamp fot unique name
$this->load->library('upload', $config);
if (!$this->upload->do_upload('image')) //upload and validate
{
$data['inputerror'][] = 'image';
$data['error_string'][] = 'Upload error: ' . $this->upload->display_errors('', ''); //show ajax error
$data['status'] = FALSE;
echo json_encode($data);
exit();
}
return $this->upload->data('file_name');
}
private function _do_upload_video()
{
$config['upload_path'] = './assets/img/sni';
$config['allowed_types'] = 'mp4';
$config['max_size'] = 9000148; //set max size allowed in Kilobyte
$config['file_name'] = uniqid(); //just milisecond timestamp fot unique name
$this->load->library('upload', $config);
if (!$this->upload->do_upload('video')) //upload and validate
{
$data['inputerror'][] = 'video';
$data['error_string'][] = 'Upload error: ' . $this->upload->display_errors('', ''); //show ajax error
$data['status'] = FALSE;
echo json_encode($data);
exit();
}
return $this->upload->data('file_name');
}
public function add_data()
{
$this->_validate();
$data = array(
'kode_process' => $this->input->post('kode'),
'process_name' => $this->input->post('name'),
'remark' => $this->input->post('remark'),
'category' => $this->input->post('category'),
'cycle_time' => $this->input->post('cycle'),
'smv' => $this->input->post('smv'),
'total' => $this->input->post('total'),
);
if (!empty($_FILES['image']['name'])) {
$upload = $this->_do_upload();
$data['image'] = $upload;
}
if (!empty($_FILES['video']['name'])) {
$upload = $this->_do_upload_video();
$data['video'] = $upload;
}
$this->Sni_model->save($data);
echo json_encode(array("status" => TRUE));
Screenshot:

Related

How to upload multiple image in few records in database using codeigniter?

I have a problem with multiple image upload in codeigniter. I want to insert to each field in database.
I've tried to upload single a file but when I change to multiple upload, it's not working.
This is my controller to add the whole data from upload images:
public function addprod(){
$item->gambar_produk = null;
$item->thumb_produk1 = null;
$data = array(
'page' => 'addprod',
'row' => $item
);
$data['title'] = 'Form Add';
$this->load->view('admin/addprod', $data);
}
This is My Controller to add images and this is my first images controller:
$config['upload_path'] = './assets/upload/images/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2400';
$config['max_width'] = '2024';
$config['max_height'] = '2024';
$this->load->library('upload', $config);
if ($this->upload->do_upload('image')){
$post['image'] = $this->upload->data('file_name');
$this->Model_product->add($post);
redirect('Product/viewprod');
} else {
$error = $this->upload->display_errors();
$this->session->flashdata('error', $error);
redirect('Product/addprod');
}
and this is my second images:
$config2['upload_path'] = './assets/upload/images/';
$config2['allowed_types'] = 'gif|jpg|png|jpeg';
$config2['max_size'] = '2400';
$config2['max_width'] = '2024';
$config2['max_height'] = '2024';
$this->load->library('upload', $config2);
if ($this->upload->do_upload('image2')){
$post['image2'] = $this->upload->data('file_name');
$this->Model_product->add($post);
redirect('Product/viewprod');
} else {
redirect('Product/addprod');
}
this is my model:
public function add($post){
$data = [
'image' => $post['image'],
'image2' => $post['image2']
];
$this->db->insert('tb_prod',$data);
}

PDF File not uploading

I have a Input Field which have same name. pdf[]
I am inserting and uploading it into database it is perfectly inserting into database. but not uploading into uploads folder there is a problem with do_upload();
$items=$_FILES['pdf']['name'];
$count = count($items);
$lstid=$this->FetchData->getLastInserted();
for($i=0; $i<=$count-1; $i++){
//echo $img[$i];
$img = $_FILES['pdf']['name'][$i];
$timezone = new DateTimeZone('Asia/Calcutta');
$datetime = new DateTime("now", $timezone);
$date = $datetime->format("D_M_d_Y_H_i_s");
$ext = pathinfo($img, PATHINFO_EXTENSION);
$new_name=$i.'_'.$state_id.'_'.$date;
if(!empty($img)){
$imagenamefordatabase = $new_name.'.'.$ext;
}
else{
$imagenamefordatabase ='';
}
$config['upload_path'] = 'uploads/';
// set the filter image types
$config['allowed_types'] = 'pdf';
$config['file_name'] = $new_name;
//load the upload library
$this->load->library('upload', $config);
$this->upload->initialize($config);
$this->upload->set_allowed_types('*');
$data['upload_data'] = '';
//if not successful, set the error message
if (!$this->upload->do_upload('pdf[]')) {
$data = array('Success' => $this->upload->display_errors());
}
else {
//else, set the success message
$data = array('Success' => "Upload success!");
$data['upload_data'] = $this->upload->data();
}
$sampleresult = array(
'Report_Print_Data_Id' => $lstid,
'Sample_Report' => $imagenamefordatabase
);
$this->AddData->addReportResult($sampleresult);
}
Change The filed name As userfile.
config file size in controller and try.
if any error please show.
You have to pass field name only without large brackets []. Update the following part of your code.
if (!$this->upload->do_upload('pdf')) {
$data = array('Success' => $this->upload->display_errors());
}
else
{
//else, set the success message
$data = array('Success' => "Upload success!");
$data['upload_data'] = $this->upload->data();
}

Fixed size Image uploading in codeigniter

I want upload image with fixed size in codeigniter.But my code is not working.Anyone please help me for doing this..My code is
public function createevent() {
if ($this->input->post('submit')) {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_width'] = '366';
$config['max_height'] = '269';
$this->load->library('upload', $config);
// $data = array('upload_data' => $this->upload->data());
$data = $this->upload->data();
$logo = $data['file_name'];
$fileInfo = getimagesize($data);
if( $fileInfo['width'] == 366 && $fileInfo['height'] == 269 ) {
// if(($fileInfo[0] == 366) && ($fileInfo[1] == 269 )){
// if ($this->upload->do_upload()) {
$this->event_model->createevent( $logo);
$this->session->set_flashdata('msg', 'Event Created');
} else {
// $this->image_lib->resize();
$this->session->set_flashdata('error', 'Please upload image within the size limit');
redirect('event/events');
}
}
}
Please use $fileInfo['image_width'] & $fileInfo['image_height'] instead of
$fileInfo['width'] & $fileInfo['height']
This will work. :)

my image is not get inserted while uploading using codeigniter

My image is not get inserted while uploading using codeigniter:
function add_newblog()
{
$sess_id = $this->session->userdata('id');
$result['query'] = $this->login_model->profile($sess_id);
foreach($result['query'] as $row)
{
$email = $row->blogger_email;
$url = $row->blogger_url;
$author = $row->blogger_name;
if ($this->input->post('submit')) {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$error = array('error' => $this->upload->display_errors());
$media = 'No Image Uploaded';
$title = $this->input->post("title");
$category = $this->input->post('category');
$content = $this->input->post('content');
$this->blog_model->add_newblog($sess_id,$title,$category,$content,$media,$email,$url,$author);
$this->session->set_flashdata('valid', 'Blog Created without Image');
redirect('content_writer/add_blog');
}
else {
$data = array('upload_data' => $this->upload->data());
$data = $this->upload->data();
$media = $data['file_name'];
$title = $this->input->post("title");
$category = $this->input->post('category');
$content = $this->input->post('content');
$this->blog_model->add_newblog($sess_id,$title,$category,$content,$media,$email,$url,$author);
$this->session->set_flashdata('valid', 'Blog Created');
redirect('content_writer/add_blog');
}
}
else{
$this->session->set_flashdata('invalid', 'Invalid');
redirect('content_writer/add_blog');
}
}
}
The else condition always works. The image name does not get saved in the image path.
the problem is here in this line
$config['upload_path'] = './uploads/';
instead use
$config['upload_path'] = 'uploads/<folder name>';
OR incase you want to save the image directly in uploads folder just use
$config['upload_path'] = 'uploads/';
as well as make your that you form in the veiw section is open with form_open_multipart()
hope this will solve your problem
Set your max_size, max_width and max_height. This would be an example below.
$config['max_size'] = '3000';
$config['max_width'] = '1500';
$config['max_height'] = '1500';
You havent passed the image name in $this->upload->do_upload() method.
use your
input type='file' name=myDoc
$this->upload->do_upload("myDoc")

Codeigniter after renaming file for upload how do i save the new file name to my database

I have an issue with codeigniter file upload library. I am trying to upload multiple files (7 images), i renamed them and after which uploaded to my server file system. But i cannot get the new name to save in my database.
Here is my Controller
if (isset($_POST['carform']))
{
$dir = './uploads/cars';
$config['upload_path'] = $dir;
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = '2048';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['remove_spaces'] = 'TRUE';
$uid = $this->tank_auth->get_user_id();
$config['file_name'] = time().$uid;
$this->load->library('upload', $config);
foreach($_FILES as $field => $file)
{
if($file['error'] == 0)
{
// So lets upload
if ($this->upload->do_upload($field))
{
$data = array('upload' => $this->upload->data());
$this->cars_model->set_car($data);//end foreach loop.....
$id = $this->db->insert_id();
redirect('/cars/details/'.$id.'');
}else
{
$errors = $this->upload->display_errors();
echo($errors);
}
}
}
}//end if statement ...
You do have a helper function within Codeigniter File Uploading Class: $this->upload->data(). In order to get the filename do this:
$my_data=$this->upload->data();
$file_name=$my_data['filename'];
check the manual here
edit:
to insert data into database assuming $filename is a string you need first explode it into an array
$filename="14010989464.jpg 140109894641.jpg 140109894642.jpg 140109894643.jpg 140109894644.jpg 140109894645.jpg 140109894646.jpg";
$fn=explode(' ',$filename);
then you can insert the data (imagename) into the column img of your table test like this:
foreach ($fn as $key=>$val){
$data['img']=$fn[$key];
$this->db->insert('test', $data);
}
can use for
ex :
$this->load->library('upload', $config);
for($i=1;$i<=5;$i++) //5 = total image
{
if (!$this->upload->do_upload('userfile'.$i))
{
$data['error'] = array('error' => $this->upload->display_errors());
}
else
{
$upload_data = $this->upload->data();
$data[$i] = $upload_data['file_name'];
}
}
$data_x = array(
'image1' => $data['1'],
'image2' => $data['2'],
'image3' => $data['3'],
'image4' => $data['4'],
'image5' => $data['5'],
);

Categories