2012-06-12 78 views
0

我陷入了一個非常棘手的情況。我需要幫助。我可以從我的文件上傳圖片或視頻。我給自己定的圖像配置文件如下:Codeigniter:區分視頻和圖片上傳

 $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

您可以使用$ _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); 
+0

您好感謝您的響應。我仍然陷在這個問題中查看視頻文件名稱是視頻和圖像文件名稱是圖像。我如何在控制器中獲取它,然後設置配置。我仍在學習。請幫助我。再次感謝迴應。 –

+0

我在第二個代碼示例中添加了一些額外的位(請參見更新)。 $這將使用正確的設置運行上傳庫。 – Rooneyl

+0

再次感謝。當我嘗試使用echo $ config ['file_name'];它會引發錯誤:消息:未定義的索引:file_name。什麼可能是可能的錯誤? –