0

我正在尝试上传带有汽车制造商、年份、租赁和付款数据的图像以及图像上传。我的图像路径也被插入到数据库中,但我仍然不知道如何将两者结合起来。我得到一个error Array to string conversion并且它没有输入我的数据和图像上传。

汽车表:

car_make | car_model | car_year | car_lease | car_payment | imgpath
acura    | G37 Sedan |   2009   |  48       |   350       |  assets/images/acura_1.jpg

控制器:

public function car_validation_do_upload() {

        $this->load->library('form_validation');


        $this->form_validation->set_rules('car_make', 'Car Make', 'required');
        $this->form_validation->set_rules('car_model', 'Car Model', 'required');
        $this->form_validation->set_rules('car_year', 'Car Year', 'required');
        $this->form_validation->set_rules('car_lease', 'Car Lease', 'required');
        $this->form_validation->set_rules('car_payment', 'Car Payment', 'required');

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

            $config['upload_path'] = './assets/images/';
            $config['allowed_types'] = 'gif|jpeg|jpg|png';
            $this->load->library('upload', $config);




            $newRow = array("car_make" => $this->input->post('car_make'),
                        "car_model" => $this->input->post('car_model'),
                        "car_year" => $this->input->post('car_year'),
                        "car_lease" => $this->input->post('car_lease'),
                        "car_payment" => $this->input->post('car_payment'));

            $data = array('upload' => $this->upload->data());

            $result = array_merge($newRow, $data);

        $this->load->model("model_users");

        $this->model_users->insert1($result);   
        $this->session->set_flashdata('message', 'car inserted into database!');
        redirect('main/insertCar');


    }


    else {
    $this->load->view('insert_car');
    }


    }


Model:
public function insert1($data) {

        $this->db->insert("cars", $data);
        }

View
<?php 

    //echo form_open('main/car_validation_insert');
    echo form_open_multipart('main/car_validation_do_upload');  
        echo validation_errors();

        if($this->session->flashdata('message')){
  echo $this->session->flashdata('message');
  }
    ?>

    <table width="504" style="margin-left:15px;">
<tr>
<td colspan="2"><p class="table-title">Insert Car</p></td>
<td></td>

</tr>

<tr>
<td>Car Make: </td>
<td><input type="text" name="car_make" class="textbox" tabindex="1"/></td>
<td>&nbsp;&nbsp;&nbsp;</td>


</tr>
<tr>
  <td>Car Model:</td>
  <td><input type="text" name="car_model" class="textbox" tabindex="2"/></td>
  <td>&nbsp;</td>

</tr>
<tr>
  <td>Car Year:</td>
  <td><input type="text" name="car_year" class="textbox" tabindex="3"/></td>
  <td>&nbsp;</td>

</tr>
<tr>
  <td>Car Lease:</td>
  <td><input type="text" name="car_lease" class="textbox" tabindex="4"/></td>
  <td>&nbsp;</td>

</tr>
<tr>
  <td>Car Payment:</td>
  <td><input type="text" name="car_payment" class="textbox" tabindex="5"/></td>
  <td>&nbsp;</td>

</tr>

<tr>
  <td>Car Image:</td>
  <td><input type="file" name="userfile" size="20" /></td>
  <td>&nbsp;</td>

</tr>

<tr>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
</tr>

</table>


<input type="submit" name="submit" value="submit" id="button" class="button" style="margin-left:140px;"  />

<?php

echo form_close();
?>
4

1 回答 1

1

改变这个:

$data = array('upload' => $this->upload->data());

$result = array_merge($newRow, $data);

对此:

  if ($this->upload->do_upload()){
    $image_data = $this->upload->data();
    //this will create a new key with the name upload in your newRow
    //so theres no need to use the function array_merge
    $newRow['imgpath'] ='assets/images/'.$image_data['file_name'];
}
else{
    //do something here
    echo 'error uploading image'
}

$image_data 变量将包含有关您上传的图像的信息

 Array
(
    [file_name]    => mypic.jpg
    [file_type]    => image/jpeg
    [file_path]    => /path/to/your/upload/
    [full_path]    => /path/to/your/upload/jpg.jpg
    [raw_name]     => mypic
    [orig_name]    => mypic.jpg
    [client_name]  => mypic.jpg
    [file_ext]     => .jpg
    [file_size]    => 22.2
    [is_image]     => 1
    [image_width]  => 800
    [image_height] => 600
    [image_type]   => jpeg
    [image_size_str] => width="800" height="200"
)

您可以在 codeingiter 的用户指南中找到有关文件上传类的所有信息:http: //ellislab.com/codeigniter/user-guide/libraries/file_uploading.html

于 2013-03-07T21:42:41.323 回答