Home » Php » PHP Codeigniter, error: Array to string conversion on renaming uploaded file

PHP Codeigniter, error: Array to string conversion on renaming uploaded file

Posted by: admin February 25, 2020 Leave a comment

Questions:

I got an error on file upload in Codeigniter when I try to rename image file. In local server is work fine, but in live server upload success but it showing error: Array to string conversion and undefined index: userfiles which is pointed to this line:

$image_name = "image-slider" . date("Y-m-d-h-i-sa").$_FILES["userfiles"]['name'];

on model file.

This my controler:

public function uploadImages()
{
    $result = $this->M_images->uploadImages();

    if($result){
        $this->session->set_flashdata('upload_success_msg', 'Upload Success!');
    }
    else
    {
        $this->session->set_flashdata('upload_error_msg', 'Upload Error!');
    }

    redirect(base_url('backend/homepage/C_images'));
}

And this is my model:

public function uploadImages()
{
    $image_name = "image-slider" . date("Y-m-d-h-i-sa").$_FILES["userfiles"]['name'];

    $upload = array(
        'slider_title' => $this->input->post("slider-title"),
        'slider_alt' => $this->input->post("slider-alt"),
        'slider_link' => $this->input->post("slider-url"),
        'slider_desc' => $this->input->post("slider-description")
    );

    $config['upload_path'] = "./assets/uploads/sliders";
    $config['allowed_types'] = 'gif|jpeg|jpg|png';
    $config['overwrite'] = TRUE;
    $config['file_name'] = $image_name;

    $this->load->library('upload', $config);
    if ( ! $this->upload->do_upload('slider-file'))
    {
        $this->db->insert($this->tableslider, $upload);
        return $this->db->insert_id();
    }
    else
    {
        $data = $this->upload->data();
        $upload['slider_file'] = $data['file_name'];
        $this->db->insert($this->tableslider, $upload);
        return $this->db->insert_id();
    }
}

Where am I doing wrong?

How to&Answers:

Problem is in your variable change $upload into $upload_data

Before that check the files are posted or not to check undefined index

public function uploadImages(){
print_r($_FILES);exit; /*Comment after checked  */

 $image_name = "image-slider" . date("Y-m-d-h-i-sa").$_FILES["userfiles"]['name'];
 $upload_data = array(
    'slider_title' => $this->input->post("slider-title"),
    'slider_alt' => $this->input->post("slider-alt"),
    'slider_link' => $this->input->post("slider-url"),
    'slider_desc' => $this->input->post("slider-description")
);

    $config['upload_path'] = "./assets/uploads/sliders";
    $config['allowed_types'] = 'gif|jpeg|jpg|png';
    $config['overwrite'] = TRUE;
    $config['file_name'] = $image_name;

    $this->load->library('upload', $config);

 if ( ! $this->upload->do_upload('slider-file')){
    $this->db->insert($this->tableslider, $upload_data);
    return $this->db->insert_id();
}else{
    $data = $this->upload->data();
    $upload_data['slider_file'] = $data['file_name'];
    $this->db->insert($this->tableslider, $upload_data);
    return $this->db->insert_id();
  }
}