0

I have modified the code to use a foreach loop, now I can access the files names, and the print_r is printing the files names in an array like this:

Array ( [0] => Uconn.png [1] => UW_Medicine.png [2] => Yale.png ) Axis.png

but I am still getting the following error:

A PHP Error was encountered

Severity: Notice

Message: Array to string conversion

and a databse error:

Error Number: 1054

Unknown column 'Array' in 'field list'

INSERT INTO `yacht_slider` (`yacht_slide_id`, `slide`) VALUES (87, Array)

I just don't know how should I pass the looped files inside the model to upload.

I am trying to upload a featured image in one input, and multiple images in another input at the same time, I tried many ways and methods to do this, but I always end with an error, mostly Array to string conversion error.

the featured image is stored in one database table and the multiple images are stored in another table.

My current code:

HTML:

<div class="form-group">
          <label for="" class="control-label">Image</label>
          <input type="file" class="form-control" name="featured">
        </div>
        <div class="form-group">
          <label for="" class="control-label">Slider</label>
          <input type="file" class="form-control" name="userfile[]" multiple>
        </div>

Model:

public function create_yacht($yacht_img, $slider){
  $slug = url_title($this->input->post('title'));
  $data = array(
    'title' => $this->input->post('title'),
    'slug' => $slug,
    'img' => $yacht_img,
    'city' => $this->input->post('city'),
    'category' => $this->input->post('category'),
    'price' => $this->input->post('price'),
    'description' => $this->input->post('description')
  );
  $this->db->insert('yachts', $data);
  $insert_id = $this->db->insert_id();
  $data_4 = array(
    'yacht_slide_id' => $insert_id,
    'slide' => $slider
  );
  $this->db->insert('yacht_slider', $data_4);
}

Controller:

public function create_yacht(){
  $data['title'] = 'Create';
  $data['categories'] = $this->category_model->get_categories();
  $this->form_validation->set_rules('title', 'Title', 'required');
  $this->form_validation->set_rules('city', 'City', 'required');
  if($this->form_validation->run() === FALSE){
    $this->load->view('templates/admin_header');
    $this->load->view('admin/create_yacht', $data);
    $this->load->view('templates/admin_footer');
  }else{
    $config['upload_path'] = './assets/images';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '2048';
    $this->load->library('upload', $config);
    $featured = 'featured';
    if (!$this->upload->do_upload($featured)) {
      $errors = array('errors' => $this->upload->display_errors());
      $yacht_img = 'https://via.placeholder.com/1920x1080';
    }else{
      $data = array('upload_data' => $this->upload->data());
      $yacht_img = $_FILES['featured']['name'];
    }
    foreach ($_FILES['userfile']['name'] as $name) {
      $this->upload->initialize($config);
      $this->upload->do_upload($name);
      $data = array('upload_data' => $this->upload->data());
      $slider = $_FILES['userfile']['name'];
    }
    print_r($slider);
    print_r($yacht_img);
    $this->yacht_model->create_yacht($yacht_img, $slider);
    // redirect('admin');
  }
}
Ahmad Tahhan
  • 125
  • 14
  • Possible duplicate of [Multiple files upload (Array) with CodeIgniter 2.0](https://stackoverflow.com/questions/11524356/multiple-files-upload-array-with-codeigniter-2-0) – Alex May 29 '18 at 03:42
  • or even: https://stackoverflow.com/questions/8377218/upload-multiple-files-in-codeigniter The only difference is you need to have your above code then a section for the multi files. For that you need a *different* upload field name. – Alex May 29 '18 at 03:42
  • I tried this solution, it did not work for me, uploading one file works fine, but alongside the one file I need to upload multiple files – Ahmad Tahhan May 29 '18 at 10:17

2 Answers2

1

I've cleaned up your code a bit and put in some error reporting so users won't be befuddled when an error occurs.

Controller:

public function create_yacht() {
        $data['title'] = 'Create';
        $data['categories'] = $this->category_model->get_categories();
        $this->form_validation->set_rules('title', 'Title', 'required');
        $this->form_validation->set_rules('city', 'City', 'required');
        if ($this->form_validation->run() === FALSE) {
            $this->load->view('templates/admin_header');
            $this->load->view('admin/create_yacht', $data);
            $this->load->view('templates/admin_footer');
        } else {
            $this->load->library('upload');
            $upload_path = './testupload/';
            // just in case, make path if it doesn't exist
            // if we can't die
            if (!is_dir($upload_path) && @mkdir($upload_path, DIR_WRITE_MODE) == false) {
                show_error('Could not make path!');
            }
            $config['upload_path'] = $upload_path;
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '2048';
            $config['file_ext_tolower'] = true;
            //$config['encrypt_name'] = true; // might be a good idea
            $this->upload->initialize($config);
            // featured image
            if (!$this->upload->do_upload('featured')) {
                show_error($this->upload->display_errors());
            }
            $yacht_img = $this->upload->data('file_name');
            // multi images
            $slider_images = array();
            $multi_files = $_FILES['userfile'];
            if (!empty($multi_files['name'][0])) { // if slider images are required remove this if
                $multi_count = count($_FILES['userfile']['name']);
                for ($i = 0; $i < $multi_count; $i++) {
                    $_FILES['userfile']['name'] = $multi_files['name'][$i];
                    $_FILES['userfile']['type'] = $multi_files['type'][$i];
                    $_FILES['userfile']['tmp_name'] = $multi_files['tmp_name'][$i];
                    $_FILES['userfile']['error'] = $multi_files['error'][$i];
                    $_FILES['userfile']['size'] = $multi_files['size'][$i];
                    if (!$this->upload->do_upload()) {
                        // failure cleanup to prevent orphans
                        @unlink($upload_path . $yacht_img);
                        if (count($slider_images) > 0) {
                            foreach ($slider_images as $image) {
                                @unlink($upload_path . $image);
                            }
                        }
                        show_error($this->upload->display_errors());
                    }
                    $slider_images[] = $this->upload->data('file_name');
                }
            }
            $this->yacht_model->create_yacht($yacht_img, $slider_images);
        }
    }

Model:

public function create_yacht($yacht_img, $slider_images) {
    $slug = url_title($this->input->post('title'));
    $data = array(
        'title' => $this->input->post('title'),
        'slug' => $slug,
        'img' => $yacht_img,
        'city' => $this->input->post('city'),
        'category' => $this->input->post('category'),
        'price' => $this->input->post('price'),
        'description' => $this->input->post('description')
    );
    $this->db->insert('yachts', $data);
    $insert_id = $this->db->insert_id();
    if (count($slider_images) > 0) {
        foreach ($slider_images as $image) {
            $this->db->insert('yacht_slider', array('yacht_slide_id' => $insert_id, 'slide' => $image));
        }
    }
}
Alex
  • 9,215
  • 8
  • 39
  • 82
0

if you are using mySQL 7 then it supports json datatype and you can save as array. If your using lower version of MySQL best solution is to convert your image array into JSON format then insert that into table. For more details on JSON in MySQL you can refer https://dev.mysql.com/doc/refman/8.0/en/json.html#json-values