Image upload issue in CodeIgniter - php

I am working on image upload in CodeIgniter. In this regard I am using following code.
$config['upload_path'] = './files/';
$config['allowed_types'] = 'gif|jpg|png|doc|txt';
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile'))
{
$error = $this->upload->display_errors();
echo json_encode(array('success'=>false,'message'=>$error)); //this line is not working.
}
else
{
echo json_encode(array('success'=>true,'message'=>lang('items_successful_updating').' '. $item_data['name'],'item_id'=>$item_id));
}
If I use below code then it is working.
echo json_encode(array('success'=>false,'message'=>'abcdef'));
Why it is happening so?

$this->upload->display_errors(); returns error array with error code and error message etc. Json Encoding may cause issue sometime in case of multidimensional array. you can use error message only, what I guess from you coding structure.
Correct Way is:
$config['upload_path'] = './files/';
$config['allowed_types'] = 'gif|jpg|png|doc|txt';
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile'))
{
$error = $this->upload->display_errors();
echo json_encode(array('success'=>false,'message'=>$error['error'])); //this line is not working.
}
else
{
echo json_encode(array('success'=>true,'message'=>lang('items_successful_updating').' '. $item_data['name'],'item_id'=>$item_id));
}

Related

i wants to import csv in codeigniter framework.but showing the error like Undefined property: Cforcecontroller::$csvimport

i wants to import csv in PHP codeigniter.am getting error.
this is my controller
function importcsv() {
$data['error'] = ''; //initialize image upload error array to empty
$config['upload_path'] = './uploads/csv/';
$config['allowed_types'] = 'csv';
$config['max_size'] = '1000';
$this->load->library('upload', $config);
// If upload failed, display error
if (!$this->upload->do_upload()) {
$data['error'] = $this->upload->display_errors();
$this->load->view('cforce/enquiryform');
} else {
$file_data = $this->upload->data();
$file_path = './uploads/csv/'.$file_data['file_name'];
if ($this->csvimport->get_array($file_path)) {
$csv_array = $this->csvimport->get_array($file_path);
var_dump($csv_array);die;
foreach ($csv_array as $row) {
$insert_data = array(
'name'=>$row['name'],
'subject'=>$row['subject'],
'mark'=>$row['mark'],
);
$this->Adminpanelmodel->insert_csv($insert_data);
}
$this->session->set_flashdata('success', 'Csv Data Imported Succesfully');
redirect(base_url().'csv');
//echo "<pre>"; print_r($insert_data);
} else
$data['error'] = "Error occured";
$this->load->view('cforce/enquiryform');
}
}
either you forget to load csvimport library, or you should check in your controller "cforce" that is there any file named "enquiryform". Means you should inspect this line - $this->load->view('cforce/enquiryform');
If this not work, make your controller's first letter in uppercase.May be it'll work for you.

Codeigniter Upload function does not work

I am working on codeigniter and during creation of one of the APIs I got the issue. I tried to upload the image on Server as a file, while searching on the web, I got familiar with inbuild upload class in codeigniter. Please have a look at this code. I am sending file from Android using this tutorial.
public function upload_image_post(){
$config['upload_path'] =base_url().'/uploads/';
$config['file_name'] = rand() .'.jpg';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = 10000;
$config['remove_spaces'] = TRUE;
$config['encrypt_name'] = TRUE;
// $file = $this->input->post('file');
$this->load->library('upload', $config);
// $this->upload->initialize($config);
$file=$_FILES['uploaded_file'];
// $this->upload->do_upload($file);
if($file){
$content=array(
'image_id'=>'IMG'.rand(),
'album_id'=> 'A',
'sp_id'=>'asQ',
'image_name'=>'aAA',
'status'=>1,
'tags'=>'s');
/* This is working*/
$res = $this->db->insert('ww_portfolio_images',$content);
}else{
$content=array(
'image_id'=>'IMG'.rand(),
'album_id'=> 'not file',
'sp_id'=>'asQaaaaa',
'image_name'=>'aAA',
'status'=>1,
'tags'=>'s');
/* This is not working, Thats Obvious*/
$res = $this->db->insert('ww_portfolio_images',$content);
}
// $destinationPath=APPPATH.'public/assets/uploads/ANKO.jpg';
if($this->upload->do_upload('uploaded_file')) {
$content=array(
'image_id'=>'IMG'.rand(),
'album_id'=> 'A',
'sp_id'=>'aaaaaaaaaaaaaaaaas',
'image_name'=>'aAA',
'status'=>1,
'tags'=>'s');
/* This is not working*/
$res = $this->db->insert('ww_portfolio_images',$content);
$this->response(['result' =>'Success',] , REST_Controller::HTTP_OK);
// return ($arr_image_info['full_path']);
}
else{
$content=array(
'image_id'=>'IMG'.rand(),
'album_id'=> 'A',
'sp_id'=>'asass',
'image_name'=>'aAA',
'status'=>1,
'tags'=>'s');
$res = $this->db->insert('ww_portfolio_images',$content);
/* This is working*/
$this->response(['result' => 'ERrro'] , REST_Controller::HTTP_OK);
// $this->response(['result' =>'Image error',], 433);
}
}
I can not figure out the problem I am facing here. I am receiving a file but it does not upload.
I have also tried to use $this->upload->do_upload() instead of $this->upload->do_upload('uploaded_file') and this $config['max_size'] = '10000'; instead of this $config['max_size'] = 10000; . Please help. Any help would be greatly appreciated.
Also, when this code run through web panel, it working fine.
Better if you provide some more detail for the type of error or warning that you are observing.
There could be number of reasons.
1) base_url() gives you the public URL. You have to specify the absolute or relative path to your upload folder.
2) (if you are using an Apache Server) Your Apache user don't have the write permission to the upload folder.
3) Folder path doesn't exists.
If the first one doesn't work, please check the rest of the points. Hope this help you.
Regards
Muaaz
Try using FCPATH
$config['upload_path'] = FCPATH . '/uploads/';
Or
$config['upload_path'] = './uploads/';
Then http://www.codeigniter.com/user_guide/libraries/file_uploading.html#the-controller
<?php
class Example extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('form');
}
// Name function what every you want remember to change it on view form.
public function upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ($this->upload->do_upload('uploaded_file')) {
// Then the success stuff
$upload_data = $this->upload->data();
echo $upload_data['file_name'];
} else {
// Errors
}
}
}
On view I would use the form helper functions form_open_multipart()
<?php echo form_open_multipart('example/upload');?>
<?php echo form_upload('uploaded_file', 'Upload');?>
<?php echo form_close();?>
check your form input in the view
My form view :
<input id="document" type="file" data-browse-label="browse" name="document" data-show-upload="false" data-show-preview="false" class="form-control file" />
other can be
permission issue for the uploads folder
if the folder does not exist you have to create it
And always try to debug the code with logs
document in the do_upload is the name of the input element in my view
if ($_FILES['document']['size'] > 0) {
$this->load->library('upload');
$config['upload_path'] = 'uploads/images';
$config['allowed_types'] = '*';
$config['max_size'] = $this->allowed_file_size;
$config['overwrite'] = false;
$config['encrypt_name'] = true;
$this->upload->initialize($config);
if (!$this->upload->do_upload('document')) {
$error = $this->upload->display_errors();
$this->session->set_flashdata('error', $error);
redirect($_SERVER["HTTP_REFERER"]);
}
$photo = $this->upload->file_name;
}

Codeigniter Image uploading issue

I am working on codeigniter project. All is working on my localhost. Now i have shifted all the things on server. I am also using cloudfare.
the problem is when i try to upload image it shows me blank screen. Following is my controller code.
// Add Data
function add(){
$data['pageTitle']=$this->lang->line('siteTitle');
// Breadcrumbs
$this->breadcrumbs->push('Dashboard','admin/dashboard');
$this->breadcrumbs->push('Category List','admin/category/index');
$this->breadcrumbs->push('Add Category','admin/category/add');
// Load View
// Form Submit
$this->form_validation->set_rules('_cat_title','Title','required|is_unique[pu_project_category.c_title]');
$this->form_validation->set_rules('_cat_slug','Slug','required');
$this->form_validation->set_rules('_cat_desc','Description','required');
if($this->form_validation->run()==TRUE){
if($this->upload->do_upload('_cat_image')){
$image=$this->upload->data();
$image=$image['file_name'];
}else{
$image='No Image';
}
$data['resUpload']=$this->upload->display_errors();
$data['res']=$this->Category_Model->add($image);
}
// Load View
$this->load->view('admin/common/header',$data);
$this->load->view('admin/common/left-sidebar',$data);
$this->load->view('admin/category/add',$data);
$this->load->view('admin/common/footer',$data);
}
Following is configuration code for uploading file,
// Upload Configuration
$config['upload_path'] = './project_images/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|zip';
$config['max_size'] = '2000';
$this->load->library('upload', $config);
$this->upload->initialize($config);
Please help me to solve this problem and tell me where i doing wrong.
try this
public function add()
{
$this->form_validation->set_rules('_cat_slug','Slug','required');
if($this->form_validation->run() == TRUE)
{
$this->load->model('welcome_model');//change this with your model
//image upload
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'png|gif|jpg|jpeg';
$config['max_size'] = '7000';
$this->load->library('upload',$config);
if ( ! $this->upload->do_upload('image'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_view', $error); //loads the view display.php with error
}
$upload_data=$this->upload->data();
$image=$upload_data['file_name'];
$this->load->view('upload_view');
$data= array(
'_cat_slug' => $this->input->post('_cat_slug'),
'image' => $image
); //loads view display.php with data
$this->welcome_model->registre($data);//change this with your model function name
}
else
{
echo validation_errors();
}
}

Images are not uploading

i am trying to upload an image in codigniter but unable to do so when I try it shows me an error array to string conversion
<?php
public function do_upload() {
$config['upload_path'] = base_url().'assets/uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->upload->do_upload();
if (!$this->upload->do_upload()) {
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata('success_msg', $error);
redirect('admin/theme');
} else {
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
?>
base_url() is return HTTP path not directory path, you should use directory path for upload content.
change
$config['upload_path'] = base_url().'assets/uploads/';
to
$config['upload_path'] = getcwd().'assets/uploads/';
you can use getcwd() native PHP function to get project root directory, and CI is also provides constant variable FCPATH for root directory.
Array to string conversion usually shows up, when you try to echo an Array.
so it seems, that the problem lies on this line:
echo validation_errors();
To avoid this error but still print out the values in the array use Foreach().
This Code should solve your problem:
foreach (validation_errors() as $error) {
//I assume you get an array as a return value
echo $error;
}
EDIT
This answer doesn't help anymore, as the author edited his question
You are uploding your files two time. After first uploding every thing is vanished and you have nothing to upload
$this->upload->do_upload();// first time
if (!$this->upload->do_upload()) {// second time
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata('success_msg', $error);
redirect('admin/theme');
}
Just comment it
// $this->upload->do_upload();// comment this line
EDIT
You can send your message into set_flashdata() as
if (!$this->upload->do_upload('file_name')) {// add file name here
$error = $this->upload->display_errors();// remove array from here
$this->session->set_flashdata('success_msg', $error);
redirect('admin/theme');
}
To read a flashdata variable:
$this->session->flashdata('success_msg');

Image Type Validation in codeigniter

I am new to Codeigniter. Working on an form validation having image in the form. Using Module MVC extension in CodeIgniter.
//for upooading image I found this file upload snippet
$config['upload_path'] = './uploads/audio_files/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '2048';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload($file_name))
{
$error = array('error' => $this->upload->display_errors());
$file1 = '';
}
else
{
$upload_data = $this->upload->data();
$file1 = $upload_data['file_name'];
}
This is working fine. File which is not jpeg | gif is not uploaded. but user dont get any error message. I want the user to show that particular error when other image type is uploaded. Please help
I tried this but failed.
try 1 : $this->template->load_partial('admin/admin_master', 'admin/form', $error);
try 2. : loading view with $error
Thanks
Can't you associate a view with an error message? For instance:
After
$file1 = '';
You put:
$this->load->view('error_view', $error, $file1);
You would have to make a separate view page called 'error_view.php'.

Categories