0

我可以从我的文件上传图像或视频。我已将图像的配置文件设置如下:

        $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);
4

1 回答 1

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);
于 2012-06-12T07:24:30.477 回答