无法从codeigniter中的文件字段访问图像

时间:2018-01-22 06:40:26

标签: codeigniter-3

我无法从 codeigniter 视图中的文件字段访问图像。在我的项目表单中,帮助程序是自动加载的。当我通过form_validation检查时,发现文件字段中的图像没有达到控制器。

当我使用form_validation和模具功能检查时,我没有选择消息图像。

这是我的观点

<div id="content" class="clearfix">
    <div class="contentwrapper"><!--Content wrapper-->

        <div class="heading">

            <h3>Manage images</h3>                    



        </div><!-- End .heading-->

        <!-- Build page from here: Usual with <div class="row-fluid"></div> -->

        <div class="row-fluid">

            <div class="span6" style="width:70%; margin-left:15%;">

                <div class="box">

                    <div class="title">

                        <h4> 
                            <span>Update Image</span>
                        </h4>

                    </div>


                        <?php echo form_open_multipart('productadmin/update_image');?>

                            <div class="form-row row-fluid">
                                <div class="span12">
                                    <div class="row-fluid">
                                        <label class="form-label span4" for="textarea">image<b style="color:#F00; font-size:11px;">*</b></label>
                                        <input type="file" name="image">
                                        <span class="error">
                                                                                    </span>
                                    </div>
                                </div>
                            </div>

                            <div class="form-row row-fluid">
                                <div class="span12">
                                    <div class="row-fluid">

                                        <img  src="<?php echo base_url();?>uploads/products/<?php echo $name;?>" width="50%" height="%">

                                    </div>
                                </div>
                            </div>

                            <div class="form-actions">
                                <button type="submit" class="btn btn-info" style="float:right;">Submit</button>

                            </div>


                        <?php   echo form_close();?>

                    </div>

                </div><!-- End .box -->

            </div><!-- End .span6 --><!-- End .span6 --><!-- End .span6 -->


        </div><!-- End .row-fluid --><!-- End .row-fluid --><!-- End .row-fluid --><!-- End .row-fluid -->





    </div><!-- End contentwrapper -->





<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/jquery-ui.min.js" type="text/javascript"></script>


                   <!-- End #content -->

      <!-- End #wrapper -->

这是我的控制器

class Productadmin extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
         $this->load->model('products_model');
        $this->load->helper('security');
        $this->load->helper('url');
        if($this->session->userdata('activedata')!=true)
        {
            $this->session->set_flashdata('message','Please do login');
           redirect('admin');


        }
        error_reporting(0);
    }
public function update_image()
    {
        $id=$this->input->post('ids');
        $newname=$this->input->post('image');
       $this->form_validation->set_rules('image','Image','required');
       $this->form_validation->set_error_delimiters('','');
       if($this->form_validation->run()==false)
       {

           echo "image not selected";
           die();
         $this->template->load('adminhome','edit_image','');
       }
 else 

 {
     echo "image selected";
     die();
     $config=array(
         'upload_path'=>'./uploads/products/',
         'allowed_types'=>'jpg|png',

     );
     $this->load->library('upload',$config);
     $this->upload->do_upload('images');
     $data=$this->upload->data();
     $newimage=$data['file_name'];
     echo $newimage;
     die();
   $this->products_model->update_single_image($id,$newimage); 
   redirect('productadmin/viewimages/'.$id);

 }
    }
 }

请帮帮我

1 个答案:

答案 0 :(得分:0)

更改此规则:

$this->form_validation->set_rules('image','Image','required');

到此:

if (empty($_FILES['image']['name']))
{
    $this->form_validation->set_rules('image', 'Image', 'required');
}

所以当图像不为空时,将跳过该图像的验证规则。