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');
Related
Unable to upload these formate file in codeigniter STEP, STP, IGES, IGS, SLDPRT, 3DM, SAT or X_T
public function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|pdf|step';
$config['max_size'] = 100000000000;
//$config['max_width'] = 1024;
// $config['max_height'] = 768;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('fileUpload'))
{
$error = array('error' => $this->upload->display_errors());
print_r($error);
// echo 'error';
// $this->load->view('upload_form', $error);
}else{
$this->upload->data();
}
error :- The filetype you are attempting to upload is not allowed
for those file types, just change your config of allowed_types into this to override checking extension:
$config['allowed_types'] = "*";
Before uploading your file, codeigniter check your file's extension first, but your file types are not in application/config/mimes.php so codeigniter does not know and then always return FALSE.
Another way is to add your mimes type into mimes.php, you can check out this in this link
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));
}
I have a form including different inputs and file upload input. I want the users if they want to upload images then upload, but if they don't want to upload. Don't give an error.
if($_POST){
$config['upload_path'] = 'images/tmp';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 2048;
$config['min_width'] = 480;
$config['min_height'] = 360;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile')) {
$error = array('error' => $this->upload->display_errors());
//Sending this $error to view and if a user didnt upload image and just filled
//the other inputs it shows error. but i dont want that.
} else { }
} else {
redirect(site_url());
}
You are on right track. Just delete or comment this line
$error = array('error' => $this->upload->display_errors());
You might be send your $error to view file like below:
$this->load->view('upload_form', $error);
So Don't send your value to view file. Just call your view when image successfully uploaded below:
if ( ! $this->upload->do_upload('userfile') )
{
// $error = array('error' => $this->upload->display_errors());
/*Delete or comment this line. Do your other stuff here if image not uploaded.*/
}else{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
/* This is example. You can do anything on successfully image upload.*/
}
You can refer codeigniter manual https://www.codeigniter.com/userguide3/libraries/file_uploading.html
$config['upload_path'] = 'images/tmp';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 2048;
$config['min_width'] = 480;
$config['min_height'] = 360;
$this->load->library('upload', $config);
//$rand_str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
// $filename=md5(str_shuffle($rand_str.mt_rand()));
// $config['file_name']=$filename;
$this->upload->initialize($config);
if ( $this->upload->do_upload('userfile'))
{
//$data = array('upload_data' => $this->upload->data());
// $data["filename"]=$filename;
// $data["ad_id"]=$lid;
// $data["filename"]=$rand_name;
// $this->Ads_model->insert_image($data);
}
I solved my problem changing the Upload class to give no error. I commented the lines including "no selected files".
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();
}
}
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'.