嗨我在codeigniter中有这个上传图片,它运行良好,将文件保存到images文件夹,然后保存到数据库。我的问题是,当我不在其上选择文件并且我的当前图像将保留时,我希望这是因为这是编辑配置文件页面。我没有在任何文本框上设置任何验证。仅用于上传图像。当即将上传像.doc .exe .flv这样的非图像类型时,它是否会对此图像进行验证"不允许您尝试上传的文件类型。"我希望当我不改变图像时,它将保留当前图像。 这里是我的代码
$config['upload_path'] = 'assets/uploads/';
// set the filter image types
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '9000';
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
$this->upload->initialize($config);
$this->upload->set_allowed_types($config['allowed_types']);
$data['upload_data'] = '';
//if not successful, set the error message
if (!$this->upload->do_upload('photo')) {
$data = $this->upload->display_errors();
$this->session->set_flashdata('error', 1);
redirect('profile/edit/id/'.$ids, $data);
}else{ //else, set the success message
$data['upload_data'] = $this->upload->data();
$uploadSuccess = $data['upload_data'];
$raw_name = $uploadSuccess['file_name'];
$file_name = $raw_name;
//print_r($file_name);exit;
$image_path = 'assets/uploads/' .$file_name;
$config['image_library'] = 'gd2';
$config['source_image'] = $image_path;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 150;
$config['height'] = 110;
//$config['quality'] = '80%';
$this->load->library('image_lib', $config);
$this->image_lib->resize($image_path);
$this->image_lib->clear();
}
使用这行代码
!$this->upload->do_upload('photo')
当我上传非图像时,这也将使用这行代码进行验证。并将工作
$data = $this->upload->display_errors();
和我的观点
<?php if($this->session->flashdata('error')): ?>
<div class="alert alert-success">
<button class="close" data-dismiss="alert" type="button">×</button>
<p >The filetype you are attempting to upload is not allowed.</p>
</div>
<?php endif; ?>
我希望当我不选择文件时,它会在我的编辑页面中保留我的照片 有人可以帮我把这件事弄清楚吗?任何帮助都非常感激。谢谢
答案 0 :(得分:0)
在上传图像代码之前,或者如果您使用的是图像上传功能,则必须先设置条件,如果图像可用,则应运行该代码。例如:
if(isset($_FILES['photo']['name'])){
// image upload code here or call upload function
}
这可能对你有帮助。