【发布时间】:2016-09-05 01:22:47
【问题描述】:
我正在尝试在表单中上传图片。表格中的所有数据都可以存储到接受图像的数据库中。并且图像也没有插入到 upload_path 中。我想当用户填写所有表单信息并上传图像时,用户单击提交按钮,包括图像在内的所有数据都存储到数据库中。我的代码有什么问题?谢谢。
//view.php
<?php echo form_open_multipart('writereview/create');?>
<label for="sitename"><span>Sitename <span class="required">*</span></span><input type="text" class="input-field" name="sitename" value="" /></label>
<input type="file" name="images" size="20" /> <br />
<label><span> </span><input type="submit" value="Submit" /></label>
//控制器
public function create() //insert data
{
$data['title'] = 'Write Review'; // Capitalize the first letter
$this->load->helper(array('html', 'url', 'form'));
$this->config_model->writereview();
$this->load->view('templates/header', $data);
$this->load->view('writereview/writereview');
$this->load->view('templates/footer');
}
//模型
public function writereview()
{
$this->load->helper('url');
$type = explode('.', $_FILES["pic"]["name"]);
$type = strtolower($type[count($type)-1]);
$url = "./assets/images/".uniqid(rand()).'.'.$type;
if(in_array($type, array("jpg", "jpeg", "gif", "png")))
if(is_uploaded_file($_FILES["pic"]["tmp_name"]))
if(move_uploaded_file($_FILES["pic"]["tmp_name"],$url))
return $url;
$data = array(
'sitename' => $this->input->post('sitename'),
'images'=>$this->input->post('images')
);
return $this->db->insert('review', $data);
}
【问题讨论】:
-
文件名是
name="images"而你是$_FILES["pic"]这是错误的。使用$_FILES["images"] -
对不起。它也不起作用..
-
对于单个上传图像,它工作正常。但是当添加其他表单数据时。它只存储表单数据而不存储图像。
标签: php codeigniter