我使用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>
请指教。谢谢。
答案 0 :(得分:2)
你错过了以下一行
$this->upload->do_upload("image")
之前添加上述代码,
$image_data = $this->upload->data('photo');
希望这可以帮助你。
答案 1 :(得分: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');