Can not upload with some different files on codeigniter - php

I try to upload data with different type of extension with codeigniter but I have constraints file it does not go into the folder that I go. i try to make some config according to what i need, please help ..
function add_module_external(){
$id_module = '9090909';
$file_data = array();
$file_data2 = array();
$image_data = array();
$config_video = array();
$config_image["file_name"] = $id_module;
$config_image["upload_path"] = "./assets/file/image-module/";
$config_image["overwrite"] = TRUE;
$config_image["allowed_types"] = "gif|jpg|png|";
$this->load->library('upload', $config_image);
$this->upload->initialize($config_image);
$upload_background = $this->upload->do_upload('other_image');
if($upload_background){
$image_data = $this->upload->data();
$image = $image_data['file_name'];
}else{
$image = $this->input->post('background');
}
// pdf and ppt upload
$config_file['file_name'] = $id_module;
$config_file['upload_path'] = '/assets/file/materi-module/';
$config_file['overwrite'] = TRUE;
$config_file['allowed_types'] = 'pdf';
$this->load->library('upload', $config_file);
$this->upload->initialize($config_file);
$upload_pdf = $this->upload->do_upload('pdf_data');
if($upload_pdf){
$file_data = $this->upload->data();
$content = $file_data["file_name"];
}else if($upload_ppt){
$file_data2 = $this->upload->data();
$content = $file_data2["file_name"];
}else if($upload_video){
$config_video = $this->upload->data();
$content = $config_video["file_name"];
}else{
$content = $this->input->post("text_data");
}
$query = $this->my_module->new_module($image, $id_module, $content);
redirect($this->agent->referrer());
}

Related

How do I upload multiple images and text file in a single form with the different button in Codeigniter?

I have a form which contains two choose file buttons one for image and second one is for file upload but in it, my image functionality is working correctly in it but when I upload my doc file this is not working properly.Please help me
$count = count($_FILES['userfile']['size']);
foreach($_FILES as $key=>$value)
for($s=0; $s<=$count-1; $s++) {
$_FILES['userfile']['name']=$value['name'][$s];
$_FILES['userfile']['type'] = $value['type'][$s];
$_FILES['userfile']['tmp_name'] = $value['tmp_name'][$s];
$_FILES['userfile']['error'] = $value['error'][$s];
$_FILES['userfile']['size'] = $value['size'][$s];
$config['upload_path'] = './upload/article_img/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
$this->upload->do_upload();
$data = $this->upload->data();
$name_array[] = $data['file_name'];
}
$names= implode(',', $name_array);
$this->load->database();
$db_data = array(
'article_id'=> $is_product_saved,
'image_title'=>json_encode($this->input->post('image_title')),
'file_name'=> $names,
'created_date' => date('Y-m-d H:i:s')
);
$is_saved_img = $this->site_model->save_data('article_image',$db_data);
if($_FILES["files"]["name"] != ''){
$output = '';
$config["upload_path"] = './upload/article_doc/';
$config["allowed_types"] = 'txt|pdf|doc';
//$this->load->library('upload', $config);
$this->upload->initialize($config);
for($count = 0; $count<count($_FILES["files"]["name"]); $count++)
{
$_FILES["file"]["name"] = $_FILES["files"]["name"][$count];
$_FILES["file"]["type"] = $_FILES["files"]["type"][$count];
$_FILES["file"]["tmp_name"] = $_FILES["files"]["tmp_name"][$count];
$_FILES["file"]["error"] = $_FILES["files"]["error"][$count];
$_FILES["file"]["size"] = $_FILES["files"]["size"][$count];
if($this->upload->do_upload('file'))
{
$data = $this->upload->data();
$output[] =$data["file_name"];
}
}
$name_doc= implode(',', $output);
$db = array(
'doc_name'=> $name_doc,
'article_id'=> $is_product_saved,
'doc_title'=>json_encode($this->input->post('document_title')),
'created_date' => date('Y-m-d H:i:s')
);
$is_saved_img = $this->site_model->save_data('article_document',$db);
}
remove } from if statement its work
if($this->upload->do_upload('file'))
{
$data = $this->upload->data();
$output[] =$data["file_name"];
}
// } remove this extra bracket because its end `for loop`

How to Upload excel data to mysql database using codeigniter?

I have codeigniter application, Now i need to Upload excel using the codigniter.
How to do that. I follow some Online tutorials, but there are not complete one. Specially there are no what is the library they use.
if(isset($_POST['Submit']))
{
$mimes = array('application/vnd.ms-excel','text/xls','text/xlsx','application/vnd.oasis.opendocument.spreadsheet','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
if(in_array($_FILES["file"]["type"],$mimes))
{
$uploadFilePath = 'uploads/'.basename($_FILES['file']['name']);
$filename = pathinfo($uploadFilePath);
$f = array("name"=>$filename['filename']);
foreach ($f as $k)
{
$policy = $k;
}
move_uploaded_file($_FILES['file']['tmp_name'], $uploadFilePath);
$Reader = new SpreadsheetReader($uploadFilePath);
$totalSheet = count($Reader->sheets());
//echo "You have total ".$totalSheet." sheets".
/* For Loop for all sheets */
for($i=0;$i<$totalSheet;$i++)
{
$Reader->ChangeSheet($i);
foreach ($Reader as $Row)
{
$treeno ++;
$policyno = isset($Row[0]) ? $Row[0] : '';
$act_treeno = isset($Row[1]) ? $Row[1] : '';
$dbh = isset($Row[2]) ? $Row[2] : '';
$height = isset($Row[3]) ? $Row[3] : '';
$pno = substr($policyno, -6);
$treeno = $pno.'_'.$act_treeno.'_T';
$query = "insert into tbl_trees(policyno,actual_treeno,dbh,height,treeno) values('".$policyno."','".$act_treeno."','".$dbh."','".$height."','".$treeno."')";
echo '</br>';
$mysqli->query($query);
this is work, but i have not idea to use this method in codeigniter.
First think you need upload that file then try open and prepare data for insert-
$fileName = $_FILES['file']['name'];
$uploadData = array();
if (!empty($fileName)) {
$config['upload_path'] = './your location/';
$config['allowed_types'] = 'txt|csv|xls|xlsx';
$config['max_size'] = 2048;
$config['remove_spaces'] = TRUE;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('file')) {
json_error("File Upload Error", $this->upload->display_errors(), 400);
} else {
$uploadData = array('upload_data' => $this->upload->data());
}
}//if
prepare data for insert-
if (!empty($uploadData)) {
$fileName = $uploadData['upload_data']['file_name'];
$data = array(); //empty array;
$fileInfo = fopen(base_url() . 'your file location/' . $fileName, "r");
$i = 0;
while (!feof($fileInfo)) {
$colInfo = fgets($fileInfo); //fgets means read file in line by line.
//prefer data to save
$data[$i]['your_column'] = $colInfo;
$i++;
}//while
fclose($fileInfo);
}
insert batch data-
$this->db->insert_batch('dbtable', $data);

can't upload image codeigniter

I am trying to upload an image.
Here is the code
<?php
$PerpetualCalendar = $this->input->post();
if($files['imgQuarter1']['name'][$key]!="")
{
$_FILES['imgQuarter1']['name']= $files['imgQuarter1']['name'][$key];
$_FILES['imgQuarter1']['type']= $files['imgQuarter1']['type'][$key];
$_FILES['imgQuarter1']['tmp_name']= $files['imgQuarter1']['tmp_name'][$key];
$_FILES['imgQuarter1']['error']= $files['imgQuarter1']['error'][$key];
$_FILES['imgQuarter1']['size']= $files['imgQuarter1']['size'][$key];
if(isset($PerpetualCalendar['id1'][$key]))
{
$img_id=$PerpetualCalendar['id1'][$key];
}
else
{
$img_id=$GetLastID;
}
$fileName = $img_id.'_NAME_'. $files['imgQuarter1']['name'][$key];
$config['file_name'] = $fileName;
$this->upload->initialize($config);
if($this->upload->do_upload('imgQuarter1'))
{
if(isset($PerpetualCalendar['NAMEpath_idQ1'][$key])){
$ImgUpdate[$imgkey]['tbl_name']='perpetual_calendar';
$ImgUpdate[$imgkey]['field_name']='event_name';
$ImgUpdate[$imgkey]['user_FK']=$user_id;
$ImgUpdate[$imgkey]['path']=$uploadpath.'/'.$fileName;
$ImgUpdate[$imgkey]['entry_id']=$img_id;
$ImgUpdate[$imgkey]['path_id']=$PerpetualCalendar['NAMEpath_idQ1'][$key]; //for update purpose
}else{
$ImgInsert[$imgkey]['tbl_name']='perpetual_calendar';
$ImgInsert[$imgkey]['field_name']='event_name';
$ImgInsert[$imgkey]['user_FK']=$user_id;
$ImgInsert[$imgkey]['path']=$uploadpath.'/'.$fileName;
$ImgInsert[$imgkey]['entry_id']=$img_id;
}
$imgkey++;
}
else
{
$this->data['error'] = $this->session->set_flashdata(array('error' => $this->upload->display_errors()));
}
}
?>
here is the View
echo form_upload(array('name'=>'imgQuarter1['.$key.']','class'=>'default'),'');
echo form_input(array('name' => 'id1['.$key.']', 'type'=>'hidden','value'=>$ValuePerpetualCalendar['cal_id']));
in above code,i can get $PerpetualCalendar['id1'][$key] this Id,but
image can not upload in the folder and can't save in Database Aswell !
Any Help?
public function upload_multiple_image($user_id){
$image = $_FILES['image']['name'];
$files = $_FILES['image'];
$image_title = $this->input->post('image_title');
$image_link = $this->input->post('image_link');
foreach($image as $key=>$data){
if($files['name'][$key]!=""){
$add_data['user_id'] = $user_id;
$add_data['title'] = $image_title[$key];
$add_data['description'] = $image_link[$key];
$add_data['status'] = 1;
$this->db->insert('user_images',$add_data);
$last_inserted_id = $this->db->insert_id();
$_FILES['image']['name'] = $files['name'][$key];
$_FILES['image']['type'] = $files['type'][$key];
$_FILES['image']['tmp_name'] = $files['tmp_name'][$key];
$_FILES['image']['error'] = $files['error'][$key];
$_FILES['image']['size'] = $files['size'][$key];
$config['upload_path'] = './user_images/original/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['overwrite'] = TRUE;
$config['file_name'] = 'place_image_'.$last_inserted_id;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload('image')){
$file_details = $this->upload->data();
$update_data = array('image'=>$file_details['file_name']);
$this->db->update('user_images',$update_data,array('id'=>$last_inserted_id));
}
}
}
}

How to upload two different files in two different directory by codeigniter?

I need some help. I want to upload two different files in two different directory by using codeigniter. I wrote the following code in my controller. but it will upload only the first image.
public function save_product() {
$data = array();
$error = array();
$config1['upload_path'] = './manager/images/products/';
$config1['allowed_types'] = 'gif|jpeg|png|jpg';
$config1['max_size'] = '3000000';
$config1['max_width'] = '1024';
$config1['max_height'] = '768';
$this->load->library('upload', $config1);
$config2['upload_path'] = './manager/images/products/large/';
$config2['allowed_types'] = 'gif|jpeg|png|jpg';
$config2['max_size'] = '3000000';
$config2['max_width'] = '1024';
$config2['max_height'] = '768';
$this->load->library('upload', $config2);
if ((!$this->upload->do_upload('product_small_image')) && (!$this->upload->do_upload('product_large_image'))){
$error = array('error' => $this->upload->display_errors());
echo "<pre>";
print_r($error);
exit();
}
else {
$fdata = $this->upload->data();
$data['product_small_image'] = 'manager/images/products/' . $fdata['file_name'];
$data['product_large_image'] = 'manager/images/products/large/' . $fdata['file_name'];
$data['product_id'] = $this->input->post('product_id', TRUE);
$data['product_name'] = $this->input->post('product_name', TRUE);
$data['category'] = $this->input->post('category', TRUE);
$result = $this->super_admin_model->save_product_detail($data);
$sdata = array();
$sdata['message'] = "Well done!</strong> You successfully add the Product Details.";
$this->session->set_userdata($sdata);
redirect('super_admin/add_product', 'refresh');
}
}
First, loading the library again with $config2 won't work because the library is already loaded once and $config1 will stay loaded. To load a new config use $this->upload->initialize($config2);
Second, loading $config2 will overwrite the previous config. You should re-arrange your code. Otherwise both uploads will only use the latest config ($config2). Example:
Load library with $config1
Process do_upload('product_small_image') and collect result
Load $confg2 using initialize()
Process do_upload('product_large_image') and collect result
Process results (save to db if success or display error if one of the uploads failed)
Here is the total code what i wrote to upload multiple image in different directory successfully. and thanks to #Samutz again.
public function save_product() {
$data = array();
$error = array();
$config1['upload_path'] = './manager/images/products/';
$config1['allowed_types'] = 'gif|jpeg|png|jpg';
$config1['max_size'] = '3000000';
$config1['max_width'] = '1024';
$config1['max_height'] = '768';
$this->load->library('upload', $config1);
if (!$this->upload->do_upload('product_small_image')){
$error = array('error' => $this->upload->display_errors());
echo "<pre>";
print_r($error);
exit();
}
else {
$fdata = $this->upload->data();
$data['product_small_image'] = 'manager/images/products/' . $fdata['file_name'];
}
$config2['upload_path'] = './manager/images/products/large/';
$config2['allowed_types'] = 'gif|jpeg|png|jpg';
$config2['max_size'] = '3000000';
$config2['max_width'] = '1024';
$config2['max_height'] = '768';
$this->upload->initialize($config2);
if (!$this->upload->do_upload('product_large_image')){
$error = array('error' => $this->upload->display_errors());
echo "<pre>";
print_r($error);
exit();
}
else {
$fdata = $this->upload->data();
$data['product_large_image'] = 'manager/images/products/large/' . $fdata['file_name'];
}
$data['product_id'] = $this->input->post('product_id', TRUE);
$data['product_name'] = $this->input->post('product_name', TRUE);
$data['category'] = $this->input->post('category', TRUE);
$result = $this->super_admin_model->save_product_detail($data);
$sdata = array();
$sdata['message'] = "Well done!</strong> You successfully add the Product Details.";
$this->session->set_userdata($sdata);
redirect('super_admin/add_product', 'refresh');
}

How to resize and create additional thumbnail in codeignite?

I am having trouble resizing photos that I am successfully uploading in my my upload module.
I am able to upload photos to the appropriate folder, however my resizing is not working and I also want to simultaneously create a thumbnail duplicate in the same folder.
public function upload()
{
$this->load->library('session');
$this->load->helper('url');
$session_id = $this->session->userdata('id');
$this->load->model('account_model');
$user = $this->account_model->user();
$data['user'] = $user;
echo $user['id'];
$user_folder = './uploads/' . $this->session->userdata('id');
if(!is_dir($user_folder)){
mkdir($user_folder, 0777);
}
$config['image_library'] = 'gd2';
$config['source_image'] = $user_folder;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 50;
$config['height'] = 50;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$config['upload_path'] = $user_folder;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '165';
$config['max_width'] = '165';
$config['max_height'] = '165';
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$data['main_content'] = '/account/upload';
$this->load->view('includes/templates/main_page_template', $data);
}
else
{
$data = array('upload_data' => $this->upload->data());
$data['main_content'] = '/account/success';
$this->load->view('includes/templates/main_page_template', $data);
}
}
Try this:
public function upload()
{
$this->load->library('session');
$this->load->helper('url');
$session_id = $this->session->userdata('id');
$this->load->model('account_model');
$user = $this->account_model->user();
$data['user'] = $user;
echo $user['id'];
$user_folder = './uploads/' . $this->session->userdata('id');
if(!is_dir($user_folder)){
mkdir($user_folder, 0777);
}
$this->load->library('image_lib'); #load the image manipulation library without initiatlising it here.
$configThumb['image_library'] = 'gd2';
//$configThumb['source_image'] = $user_folder;
$configThumb['create_thumb'] = TRUE;
$configThumb['maintain_ratio'] = TRUE;
$configThumb['width'] = 50;
$configThumb['height'] = 50;
//$this->image_lib->resize();
$config['upload_path'] = $user_folder;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '165';
$config['max_width'] = '165';
$config['max_height'] = '165';
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$data['main_content'] = '/account/upload';
$this->load->view('includes/templates/main_page_template', $data);
}
else
{
$configThumb['source_image'] = $uploadedDetails['full_path'];
$this->image_lib->initialize($configThumb); #initialize the library here
$this->image_lib->resize(); #resize is done here
$data = array('upload_data' => $this->upload->data());
$data['main_content'] = '/account/success';
$this->load->view('includes/templates/main_page_template', $data);
}
}
Full path refers to the source image that is uploaded to your server. See the else condition you will get the idea. Now, the change is that we are loading the library and initializing it after uploading the image and hence we are getting the full path from $this->upload->data().
EDIT : MY WORKING FUNCTION TO CROP AN IMAGE IN TWO DIFFERENT SIZES
function udpate_profile($userId = 0){
$data = array();
//echo "<pre>";print_r($_POST);echo "</pre>";
/* Upload Image */
if($_FILES['image']['name'] != ""){
//echo "<pre>";print_r($_FILES);echo "</pre>";
//echo "enter";die;
/* Check if previous file exists */
$chkRs = $this->db->select('image')->where('id', $this->session->userdata['logged_user']['id'])->get('admins');
//echo $this->db->last_query();die;
if($chkRs->num_rows() > 0){
$chkD = $chkRs->row_array();
if($chkD['image'] != ""){
### delete the previous image ###
$pathActual = './profile_images/';
$pathMedium = './profile_images/medium/';
$pathThumb = './profile_images/thumbs/';
if(file_exists($pathActual.$chkD['image'])){ #delete the actual image
unlink($pathActual.$chkD['image']);
}
if(file_exists($pathMedium.$chkD['image'])){ #delete the medium image
unlink($pathMedium.$chkD['image']);
}
if(file_exists($pathThumb.$chkD['image'])){ #delete the thumb image
unlink($pathThumb.$chkD['image']);
}
### delete the previous image ###
}
}
/* Check if previous file exists */
//print_r($_FILES['image']);die;
$this->load->library('image_lib');
$configUpload['upload_path'] = './profile_images/';
$configUpload['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
$configUpload['max_size'] = '0';
$configUpload['max_width'] = '0';
$configUpload['max_height'] = '0';
$configUpload['encrypt_name'] = true;
$this->load->library('upload', $configUpload);
/* size 64*72 for comments */
$configThumb = array();
$configThumb['image_library'] = 'gd2';
$configThumb['create_thumb'] = TRUE;
$configThumb['new_image'] = './profile_images/thumbs/';
$configThumb['maintain_ratio'] = TRUE;
$configThumb['width'] = 64;
$configThumb['height'] = 72;
$configThumb['thumb_marker'] = "";
//$this->load->library('image_lib');
/* size 64*72 for comments */
/* size 167*167 for profile page */
$configThumbMedium = array();
$configThumbMedium['image_library'] = 'gd2';
$configThumbMedium['create_thumb'] = TRUE;
$configThumbMedium['new_image'] = './profile_images/medium/';
$configThumbMedium['maintain_ratio'] = TRUE;
$configThumbMedium['width'] = 167;
$configThumbMedium['height'] = 167;
$configThumbMedium['thumb_marker'] = "";
/* size 167*167 for profile page */
if(!$this->upload->do_upload('image')){
return 0;
}
$uploadedDetails = $this->upload->data();
if($uploadedDetails['is_image'] == 1){
$configThumb['source_image'] = $uploadedDetails['full_path'];
$configThumbMedium['source_image'] = $uploadedDetails['full_path'];
$raw_name = $uploadedDetails['raw_name'];
$file_ext = $uploadedDetails['file_ext'];
$imgname = $raw_name.$file_ext;
$this->image_lib->initialize($configThumb);
$this->image_lib->resize();
$this->image_lib->initialize($configThumbMedium);
$this->image_lib->resize();
}
}
//die();
/* Upload Image */
$data = $this->input->post(null);
//echo "<pre>";print_r($data);echo "</pre>";die;
if(isset($imgname) && $imgname != ""){
$data['image'] = $imgname;
}
$this->db->where('id',$this->session->userdata['logged_user']['id'])->update('admins', $data);
return 1;
}
it's likely you need to pass source_image the full path of the image on the server. so if you're trying to use /uploads, then it's likely you'll need to set it as /var/www/html/uploads or whatever the full path is to that folder since the image processing is relative to the server, not the site url

Categories