Php Codeigniter未能上载,并且可以';无法获取文件名
我使用CI tank_auth library允许用户一次性编辑他们的个人资料,现在我被困在图片(头像)上传上,我希望用户上传他们的图片并将文件名存储在表中,我尝试获取文件名并在表中更新,但没有成功,具体字段留空 好的,我尝试检查控制器是否持有所选文件,我使用flashdata检查上传后的响应,我看到的是Php Codeigniter未能上载,并且可以';无法获取文件名,php,codeigniter,file-upload,Php,Codeigniter,File Upload,我使用CI tank_auth library允许用户一次性编辑他们的个人资料,现在我被困在图片(头像)上传上,我希望用户上传他们的图片并将文件名存储在表中,我尝试获取文件名并在表中更新,但没有成功,具体字段留空 好的,我尝试检查控制器是否持有所选文件,我使用flashdata检查上传后的响应,我看到的是文件名:,并且预期的$image\u data['file\u name']没有显示,即使文件没有上传到特定路径 我想知道我的代码出了什么问题,我花了几个小时寻找解决方案,但没有得到任何线索 控
文件名:
,并且预期的$image\u data['file\u 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>
查看:
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>
.
. // 诸如此类的信息字段
.
请告知。谢谢 您缺少以下行
$this->upload->do_upload("image")
在之前添加上述代码
$image_data = $this->upload->data('photo');
希望这对您有所帮助。如果您想使用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');