php 在元素中上载多个图像

6l7fqoea  于 2023-01-24  发布在  PHP
关注(0)|答案(1)|浏览(106)

我想在一个元素中上传多个图片。我一直收到“你没有选择上传文件”的消息。以下是我目前所做的;
我的html表单:

<?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(); ?>

我的控制者:

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
    }
}

我的模型:

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);
}

我的目标是能够在一个元素或数据库中的一行上传多个图像。请任何人帮助指出问题?

z3yyvxxp

z3yyvxxp1#

试试这个

$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

相关问题