-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.php
More file actions
executable file
·189 lines (158 loc) · 7.41 KB
/
Copy pathupload.php
File metadata and controls
executable file
·189 lines (158 loc) · 7.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/**
*
* author: niels.seidel@nise81.com, 2017
* htaccess file:
* php_value post_max_size 16M
* php_value upload_max_filesize 6M
* todo:
* - include this routines in moodle
*/
header('Content-Type: text/plain; charset=utf-8');
include 'util.php';
class Upload {
function __construct() {
$this->util = new Util();
$this->config = include 'config.php';
$this->result = [
"upload_max_filesize" => $this->util->convertPHPSizeToBytes( ini_get('upload_max_filesize') ),
"post_max_size" => $this->util->convertPHPSizeToBytes( ini_get('post_max_size') ),
"files" => array(),
"total-size" => 0,
"error" => ''
];
$this->HOST = $this->config['host'];
$this->HOST_PATH = $this->config['host_path'];
$this->UPLOAD_DIR = $this->config['upload_dir'];
$this->TMP_DIR = $this->config['tmp_dir'];
$this->TMP_REL_DIR = $this->config['tmp_rel_dir'];
$this->STILLS_DIR = $this->config['stills_dir'];
if( isset($_GET['completeupload']) && isset($_GET['duration'])){
$this->moveFiles($_GET['completeupload'], $_GET['duration']);
}else{
$this->upload();
}
}
/**
*
*/
function upload(){
$this->FILES = $_FILES['videofiles'];
//$MIMES = array('jpg' => 'image/jpeg','png' => 'image/png','gif' => 'image/gif' );
$this->MIMES = array('mp4' => 'video/mp4','webm' => 'video/webm' );
$this->MAX_SIZE = $this->util->getMaximumFileUploadSize(); // 200MB = 200 * 1024 * 1024;
try {
$error = 0;
switch ($this->FILES['error']) {
case UPLOAD_ERR_OK:
$error = UPLOAD_ERR_OK;
break;
case UPLOAD_ERR_NO_FILE:
//throw new RuntimeException('No file sent.');
case UPLOAD_ERR_INI_SIZE:
//$error = UPLOAD_ERR_INI_SIZE;
//break;
case UPLOAD_ERR_FORM_SIZE:
//throw new RuntimeException('Exceeded filesize limit.');
default:
//throw new RuntimeException('Unknown errors.');
}
for($key=0; $key < sizeof($this->FILES["name"]); $key++) {
if ($error == 0) {
$date = date_create();
$timestamp = date_timestamp_get($date);
$tmp_name = $this->FILES["tmp_name"][$key];
$name = $timestamp . '-' . basename($this->FILES["name"][$key]);
$n = preg_replace('/\\.[^.\\s]{3,4}$/', '', $name );
$res = [];
$res['location'] = "$this->UPLOAD_DIR/$name";
$res['name'] = $name;
$res['tmp_location'] = $this->TMP_DIR . '/' . $name;
$res['tmp_rel_location'] = $this->TMP_REL_DIR . '/' . $name;
$res['name_clean'] = $n;
$res['size'] = $this->FILES['size'][$key];
$res['type'] = $this->FILES['type'][$key];
$res['error'] = '';
$res['duration'] = $this->getVideoDuration($tmp_name);//"$this->TMP_DIR/$name");
// check mime
$finfo = finfo_open(FILEINFO_MIME_TYPE);
if ((int)$this->FILES['size'][$key] > $this->MAX_SIZE ) { // check file size
$res['error'] .= "File size for: ".$name. " (".$this->FILES['size'][$key].") is greater then ".$this->MAX_SIZE;
} else if (false === $ext = array_search( finfo_file($finfo, $tmp_name), $this->MIMES, true)){
$res['error'] .= "Invalid file format: " .finfo_file($finfo, $tmp_name);
} else if (!move_uploaded_file( $tmp_name, $this->TMP_DIR . '/' . $name )){
$res['error'] .= "Could not move file: ". $name ." to ".$this->TMP_DIR.'/'.$name;
}
$res['location'] = "$this->HOST_PATH/$name";
$this->result['files'][(int)$key] = $res;
$this->result['total-size'] = $this->result['total-size'] + $this->FILES['size'][$key];
$this->result['error'] = $res['error'];
}
}
if($this->result['total-size'] > $this->result['post_max_size']){
$this->result['error'] .= 'Max post size too high '. $this->result["total-size"] . ' of max '. $result["post_max_size"] ;
}
echo json_encode($this->result);
} catch (RuntimeException $e) {
echo json_encode($e->getMessage());
}
}
/**
* Moves the uploaded and generated files from the temporal folder to its final destination
*/
function moveFiles($name, $duration){
$error = '';
// move videos
if (is_dir($this->TMP_DIR) && is_writable($this->TMP_DIR)) {
if (is_dir($this->UPLOAD_DIR) && is_writable($this->UPLOAD_DIR)) {
$move = rename($this->TMP_DIR . '/' . $name . '.mp4', $this->UPLOAD_DIR . '/' . $name . '.mp4');
if($move == false){
$error .= 'Could not move mp4 video '. $this->UPLOAD_DIR . '/' . $name . '.mp4';
}
$move = rename($this->TMP_DIR . '/' . $name . '.webm', $this->UPLOAD_DIR . '/' . $name . '.webm');
if($move == false){
$error .= 'Could not move webm video '. $this->UPLOAD_DIR . '/' . $name . '.webm';
}
}else {
$error .= "The upload directory does not exist or is not writable.";
}
if (is_dir($this->STILLS_DIR) && is_writable($this->STILLS_DIR)) {
if (!rename(
$this->TMP_DIR . '/still-' . $name . '_comp.jpg',
$this->STILLS_DIR . '/still-' . $name . '_comp.jpg'
)){
$error .= 'Could not move thumbnail:' . $this->TMP_DIR . '/still-' . $name . '_comp.jpg';
}
if (!rename(
$this->TMP_DIR . '/still-' . $name . '_comp.gif',
$this->STILLS_DIR . '/still-' . $name . '_comp.gif'
)){
$error .= 'Could not move gif animation '.$name;
}
for($i=0; $i < $duration; $i++){
if (!rename(
$this->TMP_DIR . '/preview-' . $name . '-'. $i .'.jpg',
$this->STILLS_DIR . '/preview-' . $name . '-'. $i .'.jpg'
)){
$error .= 'Could not move preview '.$name;
}
}
}else{
$error .= $this->STILLS_DIR . 'is not writable or existing';
}
}else {
$error .= "The temporary (tmp) directory does not exist or is not writable.";
}
return $error;
}
/**
* Determines the video duration
*/
function getVideoDuration($filename){
require_once 'vendor/autoload.php';
$ffprobe = FFMpeg\FFProbe::create();
return round( $ffprobe->format( $filename )->get('duration') );
}
}
$obj = new Upload();
?>