在codeigniter中自定义文件上载的错误

时间:2014-03-22 06:59:35

标签: php codeigniter file-upload

我有两个文件上传文件:一个用于图片上传,另一个用于视频上传。现在的问题是,如果我为视频或图像上传了无效的文件类型,它会显示相同的错误

The filetype you are attempting to upload is not allowed.

我需要的是自定义错误,以便视频就像

The video you are attempting to upload is not allowed.

我怎样才能做到这一点?我正在分享视频和图像的代码部分。

查看页码:

<tr>
  <td>
    <label for="video_type">Video Genre <span class="required">*</span></label>
  </td>
  <td>
    <?php 
    $array['']="Select";
    foreach($genre as $row)
    {
      $array[$row->id] = $row->genre;
    }
    echo form_dropdown('video_type',$array, set_value('video_type'));
    ?>
  </td>
  <td>
    <?php echo form_error('video_type'); ?>
  </td>
</tr>
<tr>
  <td>
    <label for="uploader_id">Upload Video <span class="required">*</span></label>
  </td>
  <td>
    <input type="file" name="video"  class="input" value="<?php echo set_value('video'); ?>" />
  </td>
  <td>
    <?php echo form_error('video'); ?>
  </td>
</tr>

控制器:

function index()
{   
  $data['dropdown'] = lang_dropdown();  // multilanguage dropdown list
  $data['language']=$this->session->userdata('site_language');
  $userid=$this->tank_auth->get_user_id();

  if (empty($_FILES['thumb_image']['name']))
  {
    $this->form_validation->set_rules('thumb_image', 'Thumb Image', 'required|max_length[255]');  
  }

  if (empty($_FILES['video']['name']))
  {
    $this->form_validation->set_rules('video', 'Video', 'required');
  }     
  $this->form_validation->set_error_delimiters('<span class="error">', '</span>');
  $data['page']='video/upload';

  if ($this->form_validation->run() == FALSE) // validation hasn't been passed
  {
    $this->load->view('layout/template',$data);
  }
  else // passed validation proceed to post success logic
  {
    $file=$_FILES['video']['name'];
    $thumbfile=$_FILES['thumb_image']['name'];
    $form_data = array(                
                'videolink' => time().$file,
                'videothumbnail' => time().$thumbfile,
                'uploaderid' => $userid,
                'uploaderid' => set_value('1'),
    );
    $config['upload_path'] = './secure/';
    $config['allowed_types'] = 'mpeg|mp4|mpg|mpe|qt|mov|avi|movie|wmv|flv|3gp|mkv|dv|m4u|m4v|mxu|rv';
    $extn = end(explode(".", $_FILES['video']['name']));
    $config['file_name'] = time().$_FILES['video']['name'].'.'.$extn;    
    $this->load->library('upload', $config);
    $this->upload->initialize($config);
    $data['upload_data'] = '';

    if (!$this->upload->do_upload('video')) 
    {
      $data['msg'] = $this->upload->display_errors();
      $this->load->view('layout/template',$data);
    } 
    else 
    { 
      $data['upload_data'] = $this->upload->data();

      if($_FILES['thumb_image']['name']!="")
      {
        $config1['upload_path'] = './secure/';        
        $ext = end(explode(".", $_FILES['thumb_image']['name']));
        $config1['file_name'] = time().$_FILES['thumb_image']['name'].'.'.$ext;        
        $config1['allowed_types'] = 'jpg|png|jpeg|gif|bmp|jpe|tiff|tif';
        $this->load->library('upload', $config1);
        $this->upload->initialize($config1);

        if (!$this->upload->do_upload('thumb_image')) 
        {
          $data['msg'] = $this->upload->display_errors();
          $this->load->view('layout/template',$data);
        }        
      }

      if ($this->Video_model->SaveForm($form_data) == TRUE) // the information has therefore been successfully saved in the db
      {
        $data['successmsg']="Successfully uploaded the video";
      }
      else
      {
        echo 'An error occurred saving your information. Please try again later';
      }

      $this->form_validation->unset_field_data();
      $this->load->view('layout/template',$data);
    }
  }
}

1 个答案:

答案 0 :(得分:0)

试试这个

if (empty($_FILES['video']['name']))
  {
    $this->form_validation->set_message('required', 'video name incorrect'); //put this line above this statement
    $this->form_validation->set_rules('video', 'Video', 'required');
  } 
相关问题