Created
May 2, 2012 19:02
-
-
Save MauMaGau/2579290 to your computer and use it in GitHub Desktop.
CI: video upload (dump)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function upload( $gallery_id , $ajax = FALSE ){ | |
// Check if a user of any level is logged in | |
if( $this->require_min_level(1) ){ | |
$file_input = 'userfile[]'; | |
$config['encrypt_name'] = TRUE; | |
$config['upload_path'] = $this->config->item('user_upload_folder').'working/'; | |
$config['storage_path'] = $this->config->item('user_upload_folder'); | |
$config['allowed_types'] = 'mov|mpeg|mp3|avi|mp4|jpg|jpeg|gif|png|wmv'; | |
$config['max_size'] = '204800'; | |
$config['max_width'] = ''; | |
$config['max_height'] = ''; | |
$this->load->library('upload', $config); | |
$this->load->model('media_model'); | |
$this->load->model('gallery_model'); | |
$POST_MAX_SIZE = ini_get('post_max_size'); | |
$mul = substr($POST_MAX_SIZE, -1); | |
$mul = ($mul == 'M' ? 1048576 : ($mul == 'K' ? 1024 : ($mul == 'G' ? 1073741824 : 1))); | |
$allowed_video = array('video/mov','video/quicktime','video/mpeg','video/x-msvideo','video/x-flv','video/mp4','video/x-ms-wmv'); // .mov, .mpeg, .avi, .flv, .mp4, .wmv | |
$allowed_image = array('image/jpeg','image/png','image/pjpeg','image/png','image/x-png','image/gif'); | |
if(!isset($_FILES['userfiles'])){ | |
echo "File too big"; | |
} | |
// Iterate through files | |
for($i=0; $i<count($_FILES['userfiles']['name']); $i++){ | |
$_FILES['userfile']['name'] = $_FILES['userfiles']['name'][$i]; | |
$_FILES['userfile']['type'] = $_FILES['userfiles']['type'][$i]; | |
$_FILES['userfile']['tmp_name'] = $_FILES['userfiles']['tmp_name'][$i]; | |
$_FILES['userfile']['error'] = $_FILES['userfiles']['error'][$i]; | |
$_FILES['userfile']['size'] = $_FILES['userfiles']['size'][$i]; | |
//echo $_FILES['userfile']['type']; | |
// Check post max size | |
if ($_SERVER['CONTENT_LENGTH'] > $mul*(int)$POST_MAX_SIZE && $POST_MAX_SIZE){ | |
$uploaded_files[$i]['error'] = 'File size too large.'; | |
$uploaded_files[$i]['success'] = FALSE; | |
// We need to treat images and videos seperately, and filter out any files that aren't either. | |
}elseif( in_array($_FILES['userfile']['type'], $allowed_image) ){ | |
$uploaded_files[$i]['type'] = 'image'; | |
$uploaded_files[$i]['file_name'] = $this->do_upload( $gallery_id , 'image' , $config ); | |
if($uploaded_files[$i]['file_name'] !== FALSE){ | |
$uploaded_files[$i]['success'] = TRUE; | |
}else{ | |
$uploaded_files[$i]['success'] = FALSE; | |
} | |
}elseif( in_array($_FILES['userfile']['type'], $allowed_video) ){ | |
$uploaded_files[$i]['type'] = 'video'; | |
$uploaded_files[$i]['file_name'] = $this->do_upload( $gallery_id , 'video' , $config ); | |
if($uploaded_files[$i]['file_name'] !== FALSE){ | |
$uploaded_files[$i]['success'] = TRUE; | |
}else{ | |
$uploaded_files[$i]['success'] = FALSE; | |
} | |
}else{ | |
//Filetype not recognised | |
$uploaded_files[$i]['success'] = FALSE; | |
$uploaded_files[$i]['error'] = 'File type not recognised.'; // TODO list allowed types | |
} | |
} | |
foreach($uploaded_files as $uploaded_file){ | |
if( $uploaded_file['success'] === FALSE ){ | |
$view_data['error_message_stack'] = ''; | |
// Display errors if they exist | |
if(!empty($this->display_errors)){ | |
if(is_array($this->display_errors)){ | |
$view_data['error_message_stack'] = '<li>'.implode('</li><li>',$this->display_errors).'</li>'; | |
}else{ | |
$view_data['error_message_stack'] = $this->display_errors; | |
} | |
} | |
if( !empty($uploaded_file['error'])){ | |
$view_data['error_message_stack'] .= $uploaded_file['error']; | |
} | |
$media_view_data[] = array('success'=>FALSE, 'type'=>'error'); | |
}else{ | |
$type = ($uploaded_file['type'] == 'image')?'0':'1'; | |
// Get the user's user_id | |
$derived_id = $this->authentication->expose_user_id( $this->session->userdata('auth_identifier') ); | |
// Add the media to the database | |
$media_data = array( | |
'user_id' => $derived_id, | |
'gallery_id' => $gallery_id, | |
'type' => $type, | |
'file_name' => $uploaded_file['file_name'], | |
); | |
$this->media_model->add_media( $derived_id, $media_data ); | |
// Show a message indicating that the media was successfully updated | |
$view_data['confirmation'] = 1; | |
$media_data['success'] = TRUE; | |
$media_view_data[] = $media_data; | |
} | |
} | |
} | |
// Update the galleries modified date | |
$this->gallery_model->set_modified($gallery_id); | |
// If this is an ajax request then we need to send different data to the view | |
if( $ajax !== FALSE ){ | |
if(isset($view_data['error_message_stack'])){ | |
$success = TRUE; | |
$errors = $view_data['error_message_stack']; | |
}else{ | |
$success = TRUE; | |
$errors = ''; | |
} | |
// Return some JSON | |
$JSON = array( | |
'data' => $media_view_data, | |
'success' => $success, | |
'errors' => $errors | |
); | |
$data = array('JSON'=>$JSON); | |
$this->load->view( 'JSON/simple' , $data); | |
}else{ | |
$this->load->helper(array('form')); | |
// Send data to template | |
$data = array( | |
'title' => 'Gallery', | |
'content' => $this->load->view( 'gallery/gallery_edit', $view_data, TRUE ) | |
); | |
$this->load->view( $this->template, $data ); | |
} | |
} | |
private function do_upload( $gallery_id , $type , $config){ | |
$this->upload->initialize($config); | |
if(!$this->upload->do_upload()){ | |
$this->display_errors = $this->upload->display_errors(); | |
return FALSE; | |
}else{ | |
$data=array('upload_data' => $this->upload->data()); | |
$directory_path = $data['upload_data']['file_path']; | |
$directory_path_full = $data['upload_data']['full_path']; | |
$file_name = $data['upload_data']['raw_name']; | |
$save_as = $gallery_id.'_'.time().rand(0,100); | |
//$location = $config['storage_path'].$type.'/'.$gallery_id.'_'.time(); | |
// Send the file for formatting by type | |
$upload_method = "do_upload_$type"; | |
if($this->$upload_method($directory_path_full, $save_as)){ | |
return $save_as; | |
}else{ | |
return FALSE; | |
} | |
} | |
} | |
private function do_upload_image( $directory_path_full , $save_as ){ | |
//echo $directory_path_full; | |
$this->load->library('resize'); | |
// Iterate through each image size | |
foreach($this->config->item('image_size') as $image_size){ | |
$max_width = $image_size['width']; | |
$max_height = $image_size['height']; | |
$save_to = $this->config->item('user_upload_folder').'image/'.$image_size['name'].'/'.$save_as.'.jpg'; | |
$this->resize->set_file($directory_path_full); | |
$this->resize->resizeImage($max_width,$max_height,'auto'); | |
$this->resize->saveImage($save_to,80); | |
} | |
return $save_as; | |
} | |
private function do_upload_video( $directory_path_full , $save_as ){ | |
$command = "/opt/lampp/bin/php /opt/lampp/background_scripts/make_mp4.php $save_as $directory_path_full >> /opt/lampp/logs/ffmpeg_log.log 2>&1 &"; | |
exec($command); | |
return TRUE; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment