Php Codeigniter图像上传
我有一个问题,如果用户没有选择图像,他可以按“提交”底部,然后它将数据库中的名称编辑为空(因为没有图像),所以他的个人资料图片是空白的,我不希望这样。我想显示一个错误,用户没有选择图像 我的另一个问题是,当用户上载新图像时,如何从服务器上删除旧图像 以下是我的代码: 看法 控制器Php Codeigniter图像上传,php,image,codeigniter,upload,Php,Image,Codeigniter,Upload,我有一个问题,如果用户没有选择图像,他可以按“提交”底部,然后它将数据库中的名称编辑为空(因为没有图像),所以他的个人资料图片是空白的,我不希望这样。我想显示一个错误,用户没有选择图像 我的另一个问题是,当用户上载新图像时,如何从服务器上删除旧图像 以下是我的代码: 看法 控制器 function uploadImg() { $this->form_validation->set_rules('upload', 'upload', 'required');
function uploadImg()
{
$this->form_validation->set_rules('upload', 'upload',
'required');
$this->form_validation->set_rules('userfile', 'userfile',
'required');
if($this->form_validation->run() != true)
{
$this->load->model('profil_model');
$this->profil_model->addProfileImg();
redirect('profil/indstillinger/'.$this->session->userdata('username'));
}
}
CI的文件上载程序有一些基本的错误处理- 我想如果文件是空的,就会出错 至于删除图像,我假设您有存储在数据库中的图像路径: 您可以使用: 以及删除 您必须使用
unlink()
然后您可能还必须将其从数据库中删除。
您可以使用一个简单的getwhere。。
例如:
$config['query'] = $this->db->get_where('images','some id here may be');
您应该使用javascript来完成 函数验证(){ var扩展=新的
数组(“jpg”、“jpeg”、“gif”、“png”、“bmp”);
//创建数组的替代方法
var extensions=new Array(){
扩展[1]=“jpg”;扩展[0]= “jpeg”扩展名[2]=“gif”
扩展[3]=“png”;扩展[4]= “bmp”var图像_文件= document.form.image\u file.value;
var 图像长度= document.form.image\u file.value.length;
var pos=image_file.lastIndexOf('.')
+1;var ext=image\u file.substring(位置, 图像长度);
var final\u ext= ext.toLowerCase();
for(i=0;i< extensions.length;i++)
{
if(extensions[i]==final_ext){
返回true;
}
}
警报(“您必须 上载一个包含以下内容之一的图像文件: 以下扩展:+ extensions.join(“,”)+“);return 错误;
} 这样,您就不需要向服务器发出请求
**这段代码来自@Ross谢谢,但我遇到了以下错误:S致命错误:对非对象调用成员函数do_upload()in@Ross我现在已经尝试过了,但它回显“错误”如果我一直不上传,如果我上传了一张图片:S你能看到有什么问题吗?上面说上传试用版似乎无效。显示错误function@ole上传试用版?也许你是指上传路径?检查是否有效。执行
打印($data)
和打印($error)
;@Ross yes path:p,我发现了错误。。原因是我无法加载库2次。。在我的模型文件中,我使用此设置我希望图像为$this->load->library('upload',$config);(load number one);(load number one)…在我的控制器中,我使用此选项:if(!$this->upload->do_upload()),因此我需要加载“在控制器中再次上传库(加载编号2),然后它被窃听,因为你不能加载相同的2次..那么你知道我能做什么吗?”?
function uploadImg()
{
$this->form_validation->set_rules('upload', 'upload',
'required');
$this->form_validation->set_rules('userfile', 'userfile',
'required');
if($this->form_validation->run() != true)
{
$this->load->model('profil_model');
$this->profil_model->addProfileImg();
redirect('profil/indstillinger/'.$this->session->userdata('username'));
}
}
if ( ! $this->upload->do_upload()) // here
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
$path = '/path/to/my/img/upload/001.jpg'; // wherever you store this value.
unlink($path); // returns true/false on success
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
// error ..show it to user
}
else
{
$data = array('upload_data' => $this->upload->data());
// save to db
}
$config['query'] = $this->db->get_where('images','some id here may be');