在Codeigniter

时间:2016-01-07 22:13:38

标签: php codeigniter codeigniter-3

我在控制器中有2个功能。

  1. 连接到表单并进行验证。
  2. 图片上传的回调功能。
  3. 这只会在db中插入一个图像的路径。如果我从回调函数中回显print_r,它会显示所有图像,但仅来自验证函数。

        // Some Validation
        } else {
            $this->fileupload_check();
    
            $data['uploadedimage'] = $this->upload->data();
            $image_name = $data['uploadedimage']['file_name'];
            //echo '<pre>';
            //print_r($this->upload->data());
            $data['uploadedimage'] = $image_name;
    
            $this->load->model('admin/model_users');
            if($query = $this->model_users->insert_property_details($data)) {
            redirect('dashboard/property-successfully-posted');
        }
    

    回调图片上传:

    public function fileupload_check() {
    $number_of_files = sizeof($_FILES['uploadedimages']['tmp_name']);
    $files = $_FILES['uploadedimages'];
    for($i=0;$i<$number_of_files;$i++) {
      if($_FILES['uploadedimages']['error'][$i] != 0) {
        $this->form_validation->set_message('fileupload_check', 'At least 1 image needs to be uploaded in jpeg, png or gif format only.');
        return FALSE;
      }
    }
    $this->load->library('upload');
    $config['upload_path'] = FCPATH . 'uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    for ($i = 0; $i < $number_of_files; $i++) {
      $_FILES['uploadedimage']['name'] = $files['name'][$i];
      $_FILES['uploadedimage']['type'] = $files['type'][$i];
      $_FILES['uploadedimage']['tmp_name'] = $files['tmp_name'][$i];
      $_FILES['uploadedimage']['error'] = $files['error'][$i];
      $_FILES['uploadedimage']['size'] = $files['size'][$i];
      $this->upload->initialize($config);
      if ($this->upload->do_upload('uploadedimage')) {
        $data['uploadedimage'] = $this->upload->data();
        $image_name = $data['uploadedimage']['file_name'];
        $data['uploadedimage'] = $image_name;
      } else {
        $this->form_validation->set_message('fileupload_check', $this->upload->display_errors());
        return FALSE;
      }
    }
    return TRUE;
    }
    

    我试图传递来自“回拨图片上传”的价值&#39;功能到验证功能但它只传递一个图像而不是所有选择的图像。

3 个答案:

答案 0 :(得分:0)

试试这个编码...

public function fileupload_check() {

$Error_bool = TRUE;
$Error = '';

$number_of_files = sizeof($_FILES['uploadedimages']['tmp_name']);
$files = $_FILES['uploadedimages'];
for($i=0;$i<$number_of_files;$i++) {
  if($_FILES['uploadedimages']['error'][$i] != 0) {
    $Error_bool = FALSE;
    $Error = 'At least 1 image needs to be uploaded in jpeg, png or gif format only.';
  }
}
$this->load->library('upload');
$config['upload_path'] = FCPATH . 'uploads/';
$config['allowed_types'] = 'gif|jpg|png';
for ($i = 0; $i < $number_of_files; $i++) {
  $_FILES['uploadedimage']['name'] = $files['name'][$i];
  $_FILES['uploadedimage']['type'] = $files['type'][$i];
  $_FILES['uploadedimage']['tmp_name'] = $files['tmp_name'][$i];
  $_FILES['uploadedimage']['error'] = $files['error'][$i];
  $_FILES['uploadedimage']['size'] = $files['size'][$i];
  $this->upload->initialize($config);
  if ($this->upload->do_upload('uploadedimage')) {
    $data['uploadedimage'] = $this->upload->data();
    $image_name = $data['uploadedimage']['file_name'];
    $data['uploadedimage'] = $image_name;
  } else {
    $Error_bool = FALSE;
    $Error =  $this->upload->display_errors();
  }
}

if($Error_bool == FALSE) {
    $this->form_validation->set_message('fileupload_check', $Error);
    return FALSE;
} else {
    return TRUE;
}


}

答案 1 :(得分:0)

最简单的方法是创建一个serialcodes_id并在其中发送session并在另一个函数中访问该会话。

如果你想破坏会话

,你可以在之后销毁会话

答案 2 :(得分:0)

我必须对其进行一些更改才能使其正常工作,

  1. 我直接在FORM函数中包含了整个回调函数代码,并完全删除了回调函数。
  2. 这也有助于另外一件事。之前在表单验证错误的情况下,错误图像被上传到上传文件夹,但现在他们没有。

    1. 我不得不在do_upload中做一些改动(通过添加$ i&amp; implode),这有助于我在db行中插入多个图像路径。
    2. 可以在SOLVED

      找到整个脚本