how to define 2 config upload file in codeigniter - codeigniter

I want to upload 2 file with 2 config section but first file don't work and second file work good for me. how can i find problem in this code.
Please help me.
This is my code :
$this->load->library('upload');
$config['upload_path'] = './uploads/images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '200';
$config['max_width'] = '200';
$config['max_height'] = '200';
$config['file_name'] = time();
$this->upload->initialize($config); // Important
if ($this->upload->do_upload('logo')) {
$logo = $this->upload->data();
} elseif ($this->input->post('logo')) {
$logo['file_name'] = $this->input->post('logo');
} else {
$logo['file_name'] = '';
}
//===================================================
$config['upload_path'] = './uploads/images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '200';
$config['max_width'] = '950';
$config['max_height'] = '250';
$config['file_name'] = time();
$this->upload->initialize($config); // Important
if ($this->upload->do_upload('header')) {
$header = $this->upload->data();
} elseif($this->input->post('header')) {
$header['file_name'] = $this->input->post('header');
} else {
$header['file_name'] = '';
}

Related

How to validate upload file only pdf

I would like to validate file upload, the condition is file input can upload file if file input is empty, but if file input does not match the type of file setting then the error showing. How to make these error. if I using this script : !$this->upload->do_upload('file') and i let file input empty, certainly the error are showing.
this is my Config upload :
$config['upload_path'] = '../../upload/file';
$config['allowed_types'] = 'pdf';
$config['file_name'] = 'file_'.time();
$config['overwrite'] = true;
$this->upload->initialize($config);
if(!$this->upload->do_upload('file')){
$respond['error'] = true;
$respond['message'] = "Error Upload".$this->upload->display_errors();
}
You could add a condition, so it will only validate if there are uploaded file :
<?php
if ( $_FILES && $_FILES['file']['name'] )
{
$config['upload_path'] = '../../upload/file';
$config['allowed_types'] = 'pdf';
$config['file_name'] = 'file_'.time();
$config['overwrite'] = true;
$this->upload->initialize($config);
if(!$this->upload->do_upload('file')){
$respond['error'] = true;
$respond['message'] = "Error Upload".$this->upload->display_errors();
}
}
This solution will work perfectly for you
$config['upload_path'] = './uploads/;
$config['allowed_types'] = 'pdf';
$config['max_size'] = 0;
$config['max_width'] = 0;
$config['max_height'] = 0;
$config['file_name'] = round(100,99); //make your file name random
$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload('file_name')){
print $this->upload->data();
}else{
print $this->upload->display_errors();
}

image not storing in folder using codeigniter

My code is given below i unable to store image in sepecfic folder in codeigniter. Image is storing database table but not store in the specific path.
Controller:
if (isset($_FILES['header_image']) && $_FILES['header_image']['name'] != "") {
$fileName="header_image_$id";
$ext= end(explode('.',$_FILES['header_image']['name']));
$file_name=$fileName.".".$ext;
$this->_upload('uploads/header_images/', "gif|jpg|png|jpeg", "header_image",$file_name);
$this->generic_model->updateRecord("blood_tips",array("header_image"=>'uploads/header_images/'.$fileName.".$ext"), array("id"=>$id));
}
private function _upload($uploadPath,$allowedTypes,$name,$file_name)
{
$config['file_name'] = $file_name;
$config['upload_path'] = $uploadPath;
$config['allowed_types'] = $allowedTypes;
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if (!$this->upload->do_upload($name))
{
return $this->upload->display_errors();
}
else
{
$data = array('upload_data' => $this->upload->data());
}
}
Your file upload parameter is different than proposed documentation .
$config['upload_path'] = './uploads/'; Notice "./" before path.
te correct way to upload file in CI, you need to make like this:
$fileName="header_image_$id";
$ext= end(explode('.',$_FILES['header_image']['name']));
$config['upload_path'] = './uploads/header_images/';
$config['file_name'] = $fileName.".".$ext;//The extension provided in the file name must also be an allowed file type.
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 0;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}

I want to resize uploaded image in codeigniter but it is not working

I want to resize uploaded image in codeigniter but it is not working with this code. Image uploading successfully but I want to resize it to 110x110 and display it !
my code is here
class Upload_photo extends CI_Controller{
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index(){
session_start();
$u = $_SESSION['username'];
$config['upload_path'] = 'user/'.$u.'/'.$filename;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '30000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload',$config);
if(!$this->upload->do_upload('file1'))
{
echo "Error". $this->upload->display_errors();
}
else {
$config['image_library'] = 'GD2';
$config['source_image'] = $config['upload_path'];
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 110;
$config['height'] = 110;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
echo 'Photo Uploaded Successfully';
}
}
}
First you upload the original image so set the configs and upload the original image, then you resize the image. With that said:
$fullPath = 'user/' . $u . '/';
$config['upload_path'] = $fullPath;
$config['file_name'] = 'theNameofFile';
$config['allowed_types'] = 'jpg|png|bmp';
$config['max_size'] = '30000';
$config['overwrite'] = FALSE;
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if($this->upload->do_upload('file1') == FALSE)
{
echo "Error:" . $this->upload->display_errors();
return;
}
At this point the the file was uploaded. Now you need to grab it and resize.
// Reset
$config = array();
$config['image_library'] = 'gd2';
$config['source_image'] = $fullPath . $this->upload->data()['file_name'];
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 100;
$config['height'] = 100;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
Finally, if you wish to, you can delete the original image.
If you want both images 1>uploded image 2>Thumbnail Image and want to store Thumbnail image to different folder you should use $config['new_image'] and path should not your url but should be absolute server path. If you don't know how to get absolute server path ? you can see my code below.
I got the solution, my code is here
class Upload_photo extends CI_Controller{
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index(){
session_start();
$u = $_SESSION['username'];
$config['upload_path'] = 'user/'.$u;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '30000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload',$config);
$filename = $_FILES['file1']['name'];
//echo $filename;
//echo $source;
if(!$this->upload->do_upload('file1'))
{
echo "Error". $this->upload->display_errors();
return;
}
$config = array();
$config['image_library'] = 'gd2';
$config['source_image'] = 'user/'.$u.'/'.$filename ;
$config['new_image']= FCPATH . 'user/'.$u.'/Thumb/';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = FALSE;
$config['width'] = 110;
$config['height'] = 110;
$this->load->library('image_lib',$config);
if(!$this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
}
}

My file type not saving on my database

I have completed file upload but if the file name is "213134.jpg" but my code store database only name "213134" without file type. Can you please help me. I have used CI for this task
function postMessage(){
$config['upload_path'] = './uploads/inbox/';
$config['allowed_types'] = 'jpg';
$config['max_size'] = '2048';
$config['max_width'] = '3000';
$config['max_height'] = '3000';
$random=rand(00000, 99999);
$id=$this->session->userdata('id');
$pic=$id*$random;
$config['file_name'] =$pic;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
}
else
{
$blog_msg ='Sorry, Picture uploaded.';
}
$u_id=$this->session->userdata('id');
$u_name=$this->session->userdata('u_fname');
$to=$this->uri->segment(3);
$date= $date=date('d-M-Y');
$message=$_POST["message"];
$data=array(
'm_from'=>$u_id,
'from_name'=>$u_name,
'm_date'=>$date,
'm_body'=>$message,
'm_to'=>$to,
'm_attach'=>$pic
);
}
$config['allowed_types'] = 'jpg|jpeg';//Change this
Method 01
$data=array(
'm_from'=>$u_id,
'from_name'=>$u_name,
'm_date'=>$date,
'm_body'=>$message,
'm_to'=>$to,
'm_attach'=>$this->input->post('pic'), //change this(in your HTML attribute name='' )
);
Method 02
or else you can try
$ext = pathinfo($pic, PATHINFO_EXTENSION); //Get the Extension
$random=rand(00000, 99999);
$id=$this->session->userdata('id');
$pic=$id*$random.$ext;//change this
$config['file_name'] =$pic;

CodeIgniter upload multiple files to multiple directories

$configUpload = array();
$configUpload['upload_path'] = './directory_1/';
$configUpload['max_size'] = '6000';
$configUpload['max_width'] = '9500';
$configUpload['max_height'] = '9500';
$configUpload['allowed_types'] = 'png|jpg|jpeg|gif|bmp';
$this->load->library('upload',$configUpload);
$upload_1 = $this->upload->do_upload('product_image');
if($upload_1 === FALSE)
$product_data = $this->upload->data();
continue;
$config = array();
$config['upload_path'] = './directory_2/';
$config['max_size'] = '5000';
$config['max_width'] = '10000';
$config['max_height'] = '10000';
$config['allowed_types'] = 'png|jpg|jpeg|gif|bmp';
$this->load->library('upload', $config);
$upload = $this->upload->do_upload('product_image_2');
When I submit to upload, image files are in the same directory (directory_1).
Need To initialize your class
$configUpload = array();
$configUpload['upload_path'] = 'uploads/canvas_uploads/upload_save/';
$configUpload['allowed_types'] = 'png|jpg|jpeg|gif|bmp';
$configUpload['file_name'] = date("Ymd") . time();
$configUpload['overwrite'] = true;
$this->load->library('upload', $configUpload);
$this->upload->initialize($configUpload);
$upload_1 = $this->upload->do_upload('file_input');
$config['upload_path'] = 'uploads/canvas_uploads';
$config['allowed_types'] = 'jpg|png|jpeg|gif|bmp|tif';
$config['file_name'] = date("Ymd") . time();
$config['overwrite'] = true;
$this->load->library('upload', $config);
$this->upload->initialize($config);
$this->upload->do_upload("file_input");

Resources