codeigniter上传图像存储数据库中的用户文件而不是图像名称

时间:2017-07-04 14:57:26

标签: codeigniter image-upload

Database output: userfile rather than image name? 而不是 控制器: 我的上传目录很好但是当我使用另一种方法并在需要时调用它它不会给我想要的输出..我非常抱歉novicity

`

 public function uploadImage()
    {       
        $config['upload_path'] = './uploads/files';
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['max_size'] = '2048';
        $config['max_width'] = '2000';
        $config['max_height'] = '2000';
        $this->load->library('upload', $config);
        if ( !$this->upload->do_upload()){
            $error = array('error' => $this->upload->display_errors());
            echo "<script>window.alert('failed to load UserFile');</script>";
        }else{
            $data = array('upload_data' => $this->upload->data());
            $post_image = $_FILES['userfile']['name'];
            echo "<script>window.alert('your image has been successfully uploaded');</script>";
        }
    }

`

public function signupPost()
{

    if ($this->form_validation->run('signup') == FALSE) {
        //fails wont continue to next page
        $this->signup_view();
    } else {
        //upload image
        $this->uploadImage();
        //insert data into the database
        $data = array(
            'userId' => $this->input->post('userId'),
            'userfile'  => $this->input->post('userfile'),
            );
        //user data has successfully signed up
        $this->User->signup_client($data);
        redirect('http://localhost/GFC/index.php/main','refresh');
    }
}

查看:

<?php 
        $attributes = array("name" => "signupform");
        $hidden = array('userId' => 'userId', 'userfile' => 'userfile');
        echo form_open_multipart("Client_Dashboard/signupPost", $attributes, $hidden);
    ?>
<div class="w3-row">
                    <input type="file" name="userfile" size="20" style="margin-left:30%; margin-top:8%;"/>
                    <span class="text-danger"><?php echo form_error('userfile'); ?></span>
                </div>

    <div class="container" style="padding:0%;">
        <br>
        <input type="submit" value="Register" class=" w3-btn w3-teal w3-large w3-hover-white w3-padding-large" />

    </div>
        <?php echo form_close(); ?>

1 个答案:

答案 0 :(得分:0)

您没有指定输入,也没有将图像名称返回到您的功能

function uploadImage() {       
  $config['upload_path'] = './uploads/files';
  $config['allowed_types'] = 'gif|jpg|png|jpeg';
  $config['max_size'] = '2048';
  $config['max_width'] = '2000';
  $config['max_height'] = '2000';
  $this->load->library('upload', $config);

  if (!$this->upload->do_upload('userfile')){
    $error = array('error' => $this->upload->display_errors());
    // var_dump($error); die; // check error
  } else {
    $fileName = $this->upload->data();
    return $fileName;
  }
}

function signupPost() {

  if ($this->form_validation->run('signup') == FALSE) {
    // fails wont continue to next page
    $this->signup_view();
  } else {
    // upload image
    $data['UserFile'] = $this->uploadImage();
    // insert data into the database
    $data = array(
       'userId' => $this->input->post('userId'),
       'password' => $this->input->post('password'),
       'emailAddress' => $this->input->post('emailAddress'),
       'userfile'  => $this->input->post('userfile'),
       'branchId'  => $this->input->post('branchId')
    );

   // user data has successfully signed up
   $this->User->signup_client($data);
   redirect('http://localhost/GFC/index.php/main', 'refresh');
 }
}