0

我使用 CI tank_auth 库允许用户一次性编辑他们的个人资料,现在我被困在图片(头像)上传,我希望用户上传他们的图片并将文件名存储在表中,我试图获取文件名并更新表,但没有成功,具体字段留空。

好的,我尝试检查控制器是否有hold选择的文件,我使用flashdata检查上传后的响应,我看到的是filename:$image_data['file_name']没有显示预期的,甚至文件没有上传到特定路径。

我想知道我的代码出了什么问题,我已经花了几个小时来解决问题,但没有得到任何线索。

控制器:

public function index()
{
    if(!$this->tank_auth->is_logged_in()) {
        redirect('/auth/login');

    }else{

        $this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean|min_length[5]|max_length[30]');
        $this->form_validation->set_rules('country', 'Country', 'trim|required|xss_clean');
        $this->form_validation->set_rules('website', 'Website', 'trim|xss_clean|max_length[30]');

                    // Here I do upload file check

        if (!empty($_FILES['photo']['name'])) {

            $config['upload_path'] = './uploads/';
            $config['allowed_types'] = 'gif|jpg|jpeg|png';
            $config['max_size'] = '100';
            $config['max_width']  = '1024';
            $config['max_height']  = '768';

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

            $image_data = $this->upload->data('photo');

            $this->session->set_flashdata('see', 'filename: '.$image_data['file_name']);

        }else{

            $this->session->set_flashdata('see', 'where is my file?');
        }


        $data['errors'] = array();

        if($this->form_validation->run()) { // validation ok

            if(!is_null($data = $this->tank_auth->update_user(
                $this->form_validation->set_value('name'),
                $this->form_validation->set_value('country'),
                $this->form_validation->set_value('website'),
                $this->form_validation->set_value($image_data['file_name'])
            ))) { // success

                $this->session->set_flashdata('msg', 'Profile has been updated');
                redirect(current_url());
            }

        }else{

            $errors = $this->tank_auth->get_error_message();
            foreach($errors as $k => $v) $data['errors'][$k] = $this->lang->line($v);
        }

        $user_id = $this->session->userdata('user_id');
        $profile = $this->users->get_profile_by_id($user_id);

        $data = array(
            'uname' => $profile->name,
            'ucountry' => $profile->country,
            'uwebsite' => $profile->website,
            'uavatar' => $profile->photo
        );
    }

    $data['title'] = $this->session->userdata('name').'\'s Account';

    $this->load->view('header', $data);
    $this->load->view('my_account', $data);
    $this->load->view('footer', $data);
}

看法:

<?php
$photo = array(
    'name' => 'photo',
    'id' => 'photo',
    'size' => 30
);

if(($this->session->flashdata('see'))){
    echo '
        <div class="alert alert-info" style="margin:5px 0;">
            <button class="close" data-dismiss="alert" type="button">×</button>
            '.$this->session->flashdata('see').'
        </div>';
}
?>

<?php echo form_open_multipart($this->uri->uri_string()); ?>
<table border="0">
  .
  . // blah blah info fields here
  .
  <tr>
    <td>
    <div><?php echo form_label('Photo', $photo['id']); ?></div>
    <div><?php echo form_upload($photo); ?></div>
    <div class="field_error"><?php echo form_error($photo['name']); ?><?php echo isset($errors[$photo['name']])?$errors[$photo['name']]:''; ?></div>
    </td>
  </tr>
</table>

<div style="padding:20px 0;"><?php echo form_submit('submit', 'Save'); ?></div>

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

请指教。谢谢。

4

2 回答 2

2

您缺少以下行

$this->upload->do_upload("image")

在前面加上上面的代码,

$image_data = $this->upload->data('photo');

希望这可以帮助你。

于 2013-09-24T09:31:36.170 回答
0

如果你想用 CI 将上传的文件写入服务器,那么试试这个

$path_to_file = $config['upload_path'].$image_data['file_name'];
write_file($config['upload_path'].$image_data['file_name'], .$image_data['file_name']);

另外,请确保您也加载文件帮助程序,如下所示

$this->load->helper('file');
于 2013-09-24T09:38:33.467 回答