【问题标题】:Codeigniter validating file upload and text input in one formCodeigniter 以一种形式验证文件上传和文本输入
【发布时间】:2012-07-08 09:55:26
【问题描述】:

我有一个带有文本输入和文件输入的表单。使用 Codeigniter 的验证库验证两种输入类型的正确方法是什么?我找到了一些解决方案,但它们不能正常工作或看起来有点矫枉过正(创建新库或修改 CI 系统文件)。

在我看来,我正在使用 1 个多部分表单并同时显示文本验证错误和上传错误。

这是我的控制器中到目前为止的内容...

function create() //create new post
{           
    $this->form_validation->set_rules('content', 'Entry', 'trim|required|xss_clean');
    $this->form_validation->set_rules('category_id', 'Category', 'trim|required|xss_clean|integer'); 

    //Text input fields
    if ($this->form_validation->run() == FALSE)
    {
        $this->load->view('new_post');
    }       
    else
    {
            $config['upload_path'] = './uploads/posts/';
            $config['allowed_types'] = 'jpg|png';               
            $config['max_size'] = '800'; //in KB

            $this->load->library('upload', $config);

            //File Upload
            if (! $this->upload->do_upload())
            {
                $upload_error['upload_error'] = array('error' => $this->upload->display_errors()); 

                $this->load->view('my_view', $upload_error);

                return FALSE;
            }

             //Add to database 
             $data = array (
               'user_id' => $this->tank_auth->get_user_id(),
               'category_id' => $this->input->post('category_id'),
               'content' => $this->input->post('content')
             );

             $this->Posts_model->create_post($data);

             $this->session->set_flashdata('success', 'Post_added!');
             redirect('posts');
    }       

}

在我看来,我不断收到You did not select a file to upload.。

【问题讨论】:

    标签: php forms validation codeigniter file-upload


    【解决方案1】:

    您的文件输入的名称是什么? do_upload() 默认情况下期望它是 'userfile',但是如果你有类似 <input type="file" name="image"... 的东西,你需要调用 $this->upload->do_upload('image')

    您还需要确保表单设置为 multipart - http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html

    【讨论】:

    • 谢谢!!我没有设置我的文件输入的名称。
    【解决方案2】:

    您正确使用了 CodeIgniter 的帮助程序,这可能更多地是 PHP 问题而不是 CodeIgniter 问题。

    看起来您的文件对于您的 PHP 配置来说可能太大了? PHP 似乎没有将文件传递给您。创建一个php_info() 文件并查看UPLOAD_MAX_FILESIZE 设置为什么?

    另外,请确保您在 application/config/mimes.php 中设置了扩展名和 mime 类型对。

    【讨论】:

    • 谢谢,我正在尝试上传允许的文件大小和类型,但仍然收到消息。当我尝试上传超过最大文件大小的文件时,我应该会收到该错误。仅处理类型文件时,在我的应用程序中上传工作。
    • 设置扩展/mime 类型对解决了我的问题。
    【解决方案3】:

    默认的codeigneter上传类是 $this->upload->do_upload('field_name = userfile') ,所以你可以设置你制作的字段文件的名称或者你使用默认的codeigneter名称字段文件,如果你想改变表单中文件类型的名称字段,请检查codeigneter如何为该类进行设置,实际上是简单的类且易于使用..,因为该类需要文件的参数名称

    【讨论】:

      【解决方案4】:

      使用 codeigniter 上传文件的表单字段验证

      控制器:Upload.php

      注意:在基础级别创建文件夹 uploads 上传文件夹存储文件..

      <?php
      
      class Upload extends CI_Controller {
      
          function __construct()
          {
              parent::__construct();
              $this->load->helper(array('form', 'url'));
          }
      
          function index()
          {
              //$this->load->view('upload_form', array('error' => ' ' ));
              $this->load->library('form_validation');
              $this->form_validation->set_rules('title', 'Title', 'trim|required|xss_clean');
      
              //Text input fields
              if ($this->form_validation->run() == FALSE)
              {
                  $this->load->view('upload_form');
              }
              else
              {
                  $config['upload_path'] = './uploads/';
                  $config['allowed_types'] = 'gif|jpg|png';
                  $config['max_size'] = '1024';
                  $config['max_width']  = '1024';
                  $config['max_height']  = '768';
      
                  $this->load->library('upload', $config);
      
                  if ( ! $this->upload->do_upload())
                  {
                      $error = array('error' => $this->upload->display_errors());
      
                      $this->load->view('upload_form', $error);
                  }
                  else
                  {
                      $data = array('upload_data' => $this->upload->data());
      
                      $this->load->view('upload_success', $data);
                  }
              }
      
          }
      }
      ?>
      

      表单:(upload.form.php)

      <html>
      <head>
      <title>Upload Form</title>
      </head>
      <body>
      
      <?php echo validation_errors(); ?>
      
      <?php echo form_open_multipart();?>
      Title: <input type="text" name="title" size="30" /><br/>
      <input type="file" name="userfile" size="20" />
      
      <br /><br />
      
      <input type="submit" value="upload" />
      
      </form>
      
      </body>
      </html>
      
      **upload_success.php**
      
      
      <html>
      <head>
      <title>Upload Form</title>
      </head>
      <body>
      
      <?php echo validation_errors(); ?>
      
      <?php echo form_open_multipart();?>
      Title: <input type="text" name="title" size="30" /><br/>
      <input type="file" name="userfile" size="20" />
      
      <br /><br />
      
      <input type="submit" value="upload" />
      
      </form>
      
      </body>
      </html>
      

      【讨论】:

        猜你喜欢
        • 2022-01-16
        • 2017-04-25
        • 2023-03-08
        • 1970-01-01
        • 2015-08-06
        • 1970-01-01
        • 2013-07-13
        • 2012-09-08
        • 2013-12-11
        相关资源
        最近更新 更多