【问题标题】:CodeIgniter - Combine form validation and file uploadCodeIgniter - 结合表单验证和文件上传
【发布时间】:2012-05-09 10:45:58
【问题描述】:

我想创建文件上传,其中包含一些字段作为上传文件的标题和摘要。我也看过this的问题,但我不完全理解。

function someform()
{
   // some variables for fields
   ($this->form_validation->run() == FALSE)
   {

   }
   else
   {
      // how to check if the file was uploaded?
   }
}

【问题讨论】:

    标签: php


    【解决方案1】:

    您应该检查除文件上传之外的所有其他字段的验证。验证成功后,检查文件。示例:

    function someform()
    {
       // some variables for fields
       ($this->form_validation->run() == TRUE)
       {
    
           $config['upload_path'] = './uploads/';
           $config['allowed_types'] = 'gif|jpg|png';
           $config['max_size'] = '100';
           $config['max_width'] = '1024';
           $config['max_height'] = '768';
    
           $this->load->library('upload', $config);
    
    
           $fileTagName = 'myfile'; // e.g <input type='file' name='myfile' />    
           if ($this->upload->do_upload($fileTagName))) {
               //Success in validation of all fields.
           }
           else {
               //Failed to upload file.
               echo $this->upload->display_errors('', '');
           }
       }
       else
       {
          //Other fields failed.
       }
    }
    

    【讨论】:

    • 我必须在视图中使用哪个函数 - form_open()、form_open_multipart() 或两者兼而有之?
    • 你应该使用'form_open_multipart'。
    猜你喜欢
    • 2012-09-08
    • 2011-07-07
    • 1970-01-01
    • 2016-10-03
    • 2015-03-12
    • 1970-01-01
    • 2011-06-29
    • 1970-01-01
    • 2015-11-16
    相关资源
    最近更新 更多