我有一个表单来编辑数据库中的几个字段,我有一个字段来上传照片。除了图像字段之外,所有文本字段都在数据库中更新,照片名称始终为null,我不知道为什么!此参数始终为null:$ profile_img。任何人都可以帮助我。我正在使用CodeIgniter。谢谢 这是我的控制器:
function superadmin_landing_content_edit() {
$content_id = $this->uri->segment(4);
if (empty($_FILES['content_img']['name']))
{
$this->form_validation->set_rules('content_img', 'Image', 'required');
}
if (isset($_POST) && !empty($_POST))
{
if(isset($_FILES["content_img"]["name"]) and !empty($_FILES["content_img"]["name"])) {
$config['upload_path'] = $this->config->config["public_path"]."landing/";
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '20000000';
$config['max_width'] = '2024';
$config['max_height'] = '2024';
$this->load->library('upload');
$this->upload->initialize($config);
$result = $this->upload->do_upload('content_img');
if($result>=1) {
$imageinfo = $this->upload->data();
$profile_img = $imageinfo["file_name"];
$profile_img = $profile_img."*".'/var/www/html/upload/home/landing/'.$profile_img; //always null
}
}
$data = array(
'name' => $this->input->post('name'),
'image_link' => $profile_img, //here always null
'type' => $this->input->post('type'),
'link' => $this->input->post('link'),
'property_type_id' => $this->input->post('prop_id'),
'orientation_id' => $this->input->post('ori_id'),
'state_id' => $this->input->post('state'),
'city_id' => $this->input->post('city'),
'flag' => $this->input->post('flag'),
);
if ($this->form_validation->run() === true)
{
$this->Admin_model->update_landing_content($content_id, $data);
}
}
$this->load->view('admin/superadmin_landing_content_edit', $data);
}
这是我的观点:
<?php $content_id = $content['id']; ?>
<?php $attributes = array("name" => "content_img_edit", "id" => "content_img_edit", "class" => "edit_view","method" => "post");
echo form_open_multipart("admin/admin/superadmin_landing_content_edit/".$content_id, $attributes); ?>
<div class="form-group">
<label>Image</label>
<?php echo form_upload(array("id"=>"content_img", "name"=>"content_img", "class"=>"form-control"))?>
</div>
<div class="form-group">
<?php echo form_submit('submit', 'Submit', 'class="btn btn-default"');?>
<input type="button" name="btnBack" class="btn btn-default" id="btnBack" value="Landing Content List" onclick="window.location.href='<?php echo base_url() ?>admin/admin/superadmin_landing_content_list'" />
</div>
<?php echo form_close(); ?>
答案 0 :(得分:1)
尝试替换
$result = $this->upload->do_upload('content_img');
if($result>=1) {
使用
if (!$this->upload->do_upload('content_img')){
$error = $this->upload->display_errors();
} else {
$data = $this->upload->data();
}
答案 1 :(得分:0)
Try like this one:
Controller:
if(!empty($_FILES['content_image']['name']))
{
// file upload code start
$config['upload_path'] ='./uploads/'; //file or image upload folder
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('content_image'))
{
echo $this->upload->display_errors();
return false;
}
else
{
$upload_data = $this->upload->data('content_image');
$content_image = $upload_data['file_name'];
$data = array('content_image'=>$content_image,
);
//$this->db->where('id',$id)->update('student',$data);
}
}