简体   繁体   中英

Uploading multiple images in on element

I want to upload multiple images in one element. I keep getting a "You did not select a file upload" message. Here's what i've done so far;

My html form:

<?php
  echo form_open_multipart('properties/create_property_ajax'); ?>
     

    <div class="form-group">
        <label class="form-control-label">Price*</label>
        <br />
        <input type="text" name="price" class="form-control" value="<?php echo                      set_value('price'); ?>" />
        <div class="form-error"><?php echo form_error('price'); ?></div>
    </div>

    <div class="form-group">
        <label class="form-control-label">Upload Featured Image </label><br />
        <small>Only JPG and PNG files allowed (max 4MB).</small>
        <input type="file" name="featured_image[]" multiple="" class="form-control" accept=".jpeg,.jpg,.png" required />
        <div class="form-error"><?php echo $error; ?></div>
    </div>


<?php echo form_close(); ?>

My Controller:

public function create_property_ajax()
{
    
    $this->form_validation->set_rules('title', 'Title', 'trim');
    $this->form_validation->set_rules('description', 'Description', 'trim|min_length[2]|max_length[800]');
    $this->form_validation->set_rules('price', 'Price', 'required');
    $this->form_validation->set_rules('state', 'State');
    $this->form_validation->set_rules('lga', 'LGA', 'required');
    $this->form_validation->set_rules('address', 'Address', 'required');
    $this->form_validation->set_rules('amenities[]', 'Other Positions');

    //config for file uploads
    $config['upload_path']          = 'assets/uploads/properties'; //path to save the files
    $config['allowed_types']        = 'jpg|JPG|jpeg|JPEG|png|PNG';  //extensions which are allowed
    $config['max_size']             = 1024 * 4; //filesize cannot exceed 4MB
    $config['file_ext_tolower']     = TRUE; //force file extension to lower case
    $config['remove_spaces']        = TRUE; //replace space in file names with underscores to avoid break
    $config['detect_mime']          = TRUE; //detect type of file to avoid code injection

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

    if ($this->form_validation->run()) {

        if ($_FILES['featured_image']['name'] == "") { //file is not selected
            $this->session->set_flashdata('status_msg_error', 'No file selected.');
            redirect(site_url('properties/new_property'));
        } elseif ((!$this->upload->do_upload('featured_image')) && ($_FILES['featured_image']['name'] != "")) {
            //upload does not happen when file is selected
            $error = array('error' => $this->upload->display_errors());
            $this->new_property($error); //reload page with upload errors

        } else { //file is selected, upload happens, everyone is happy
            $featured_image = $this->upload->data('file_name');
            //generate thumbnail of the image with dimension 500x500
            $thumbnail = generate_image_thumb($featured_image, '500', '500');
            $this->property_model->add_new_property($featured_image,     $thumbnail);
            $this->session->set_flashdata('status_msg', 'Property added and published successfully.');
            redirect(site_url('properties'));
        }
    } else {
        $this->new_property(); //validation fails, reload page with validation errors
    }
}

My Model:

public function add_new_property($featured_image, $thumbnail)
{
    $title = ucwords($this->input->post('title', TRUE));
    $description = $this->input->post('description', TRUE);
    $price = $this->input->post('price', TRUE);
    $state = ucwords($this->input->post('state', TRUE));
    $lga = $this->input->post('lga', TRUE);
    $address = $this->input->post('address', TRUE);
    $amenities = implode(", ", $this->input->post('amenities', TRUE));

    $data = array(
        'title' => $title,
        'description' => $description,
        'price' => $price,
        'state' => $state,
        'lga' => $lga,
        'address' => $address,
        'amenities' => $amenities,
        'featured_image' => $featured_image,
        'featured_image_thumb' => $thumbnail,
    );
    $this->db->insert('property', $data);
}

My goal is to be able to upload multiple images in one element or in one row in the database. Please can anyone help point out the issues?

Try this

 $this->load->library('upload'); $upload_config = array(); $upload_config['upload_path'] = 'assets/uploads/properties'; $upload_config['allowed_types'] = 'jpg|gif|jpeg|png'; $array_images_uplaoded = array(); $files = $_FILES; $multi_file = count($_FILES['featured_image']['name']); for($i=0; $i<$multi_file; $i++) { $_FILES['featured_image']['name']= $files['featured_image']['name'][$i]; $_FILES['featured_image']['type']= $files['featured_image']['type'][$i]; $this->upload->initialize($upload_config); $this->upload->do_upload('featured_image'); $array_images_uplaoded[] = $this->upload->data(); } print_r($array_images_uplaoded); // uploaded images

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM