区分CodeIgniter中的视频和图像上传

时间:2012-06-12 07:09:59

标签: codeigniter codeigniter-2

我可以从我的文件上传图片或视频。我按如下方式设置了图像的配置文件:

        $config['upload_path'] = './docs/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '200';
        $config['max_width']  = '250';
        $config['max_height']  = '250';
        $config['file_name']  = md5(rand().time());

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

但是当我上传视频时,此配置将无法正常工作,因为我需要能够上传更大尺寸的文件(视频)。在提交后找出我上传的内容(img / video)并基于此我可以启用配置文件。

视频配置如下:

            $configVideo['upload_path'] = './docs/';
            $configVideo['max_size'] = '50240';
            $configVideo['allowed_types'] = 'avi|flv|wmv|mp3|wma';
            $configVideo['overwrite'] = FALSE;
            $configVideo['remove_spaces'] = TRUE;
            $video_name = $date.$_FILES['video']['name'];
            $configVideo['file_name'] = $video_name;

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

1 个答案:

答案 0 :(得分:1)

您可以先使用 $ _ FILES 查找该类型。 未测试

$type = $_FILES['userfile']['type'];
switch ($type) {
   case 'gif':
   case 'jpg':
   case 'png':
      // do img config setup
      break;
   case 'avi':
   case 'flv':
   case 'wmv':
   case 'mp3':
   case 'wma':
      // do video config
      break;
}

或者,如果您对表单中的文件使用不同的名称,那么为什么不尝试;

[<强>更新

$config['upload_path'] = './docs/';
if (is_array($_FILES) && isset($_FILES['image']['name'])) {
    // is an image
    $config['upload_path'] = './docs/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '200';
    $config['max_width']  = '250';
    $config['max_height']  = '250';
    $config['file_name']  = md5(rand().time());
} elseif (is_array($_FILES) && isset($_FILES['video']['name'])) {
    // is a video
    $config['max_size'] = '50240';
    $config['allowed_types'] = 'avi|flv|wmv|mp3|wma';
    $config['overwrite'] = FALSE;
    $config['remove_spaces'] = TRUE;
    $video_name = $date.$_FILES['video']['name'];
    $config['file_name'] = $video_name;
}

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