将图像上传到codeigniter中的数据库?
问题描述:
我试过搜索,但没有成功,我想上传最多5个图像到数据库以及用户表单数据。我有表格,所有表单的用户数据都与上传的图像一起保存[图像上传附带用户表单]图片字段在数据库中被命名为pic1,pic2,pic3 .. pic5 +电子邮件,密码等我成功上传图像数据到数据库而不是图像。将图像上传到codeigniter中的数据库?
//控制器
if ($this->form_validation->run()!=true) {
$data['countryDrop'] = $this->Country_states_cities->getCountries();
$this->load->view('header');
$this->load->view('register',$data); //Display page
$this->load->view('footer');
}else{
$form=array();
$form['first_name']=$this->input->post("first_name",true);
$form['last_name']=$this->input->post("last_name",true);
$form['dob']=date('Y-m-d',strtotime($this->input->post("dob",true)));
$form['email']=$this->input->post("email",true);
$form['password']=sha1($this->input->post("password",true));
$form['phone']=$this->input->post("phone",true);
$form['addline2']=$this->input->post("addressline2",true);
$form['zip']=$this->input->post("zip",true);
$result = $this->Couch_reg->insert_new_user($form); //call to model
//模型
function insert_new_user($form)
{
$this->db->insert('tbl_usrs',$form);
if ($this->db->affected_rows() > 0) {
return true;
} else {
return false;
}
}
//视图
<input type="file" name="uploadfile[]" accept = "image/*" multiple = "multiple" size="20" /> <br />
我们可以看到模型部分很短,我想收集图片名称数组表单并将其发送到数据库。
答
您需要将图像保存在上传文件夹中,并将图像名称保存在数据库中。
<html>
<body>
<form method="POST" action="<?php echo site_url('my-controller/file_upload');?>" 'enctype'=>'multipart/form-data'>
<label for="file">Filename:</label>
<input type="file" name="userfile[]" id="file" multiple>
<input type="submit" value="upload"></form>
</body>
</html>
然后里面创建控制器 化妆funtion:
$files = $_FILES;
$cpt = count($_FILES['userfile']['name']);
for($i=0; $i<$cpt; $i++)
{
$_FILES['userfile']['name']= $files['userfile']['name'][$i];
$_FILES['userfile']['type']= $files['userfile']['type'][$i];
$_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
$_FILES['userfile']['error']= $files['userfile']['error'][$i];
$_FILES['userfile']['size']= $files['userfile']['size'][$i];
$this->upload->initialize($this->set_upload_options());
$this->upload->do_upload();
$fileName = $_FILES['userfile']['name'];
$images[] = $fileName;
希望这会帮助你。 欲了解更多,你可以试试这个:http://w3code.in/2015/09/upload-file-using-codeigniter/
+0
这是邪恶的地方学习我不得不做很多的更正,但它的帮助 –
+0
感谢您的反馈将尝试做得更好。 – Rajat
你看着这里http://www.codeigniter.com/user_guide/libraries/file_uploading.html – user4419336
@ wolfgang1983是的,但我想“多个图像”被上传并保存到数据库 –