CodeIgniter - 结合表单验证和文件上传

时间:2012-04-29 11:29:56

标签: php

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

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

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

1 个答案:

答案 0 :(得分:3)

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

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.
   }
}