15
15
16
16
// ------------------------------------------------------------------------
17
17
18
+ use O2System \Kernel \Http \Message \Uri \Path ;
18
19
use O2System \Psr \Http \Message \StreamInterface ;
19
20
use O2System \Psr \Http \Message \UploadedFileInterface ;
20
21
use O2System \Spl \Exceptions \Logic \BadFunctionCall \BadPhpExtensionCallException ;
@@ -62,8 +63,8 @@ class UploadFile implements UploadedFileInterface
62
63
63
64
/**
64
65
* UploadFile::$error
65
- *
66
- * @var mixed
66
+ *
67
+ * @var mixed
67
68
*/
68
69
protected $ error ;
69
70
@@ -92,20 +93,20 @@ class UploadFile implements UploadedFileInterface
92
93
*/
93
94
public function __construct (array $ uploadedFile )
94
95
{
95
- if ( ! class_exists ('finfo ' )) {
96
+ if (! class_exists ('finfo ' )) {
96
97
throw new BadPhpExtensionCallException ('E_HEADER_BADPHPEXTENSIONCALLEXCEPTION ' , 1 );
97
98
}
98
99
99
- $ this ->name = $ uploadedFile [ 'name ' ];
100
- $ this ->type = $ uploadedFile [ 'type ' ];
101
- $ this ->tmpName = $ uploadedFile [ 'tmp_name ' ];
102
- $ this ->size = $ uploadedFile [ 'size ' ];
103
- $ this ->error = $ uploadedFile [ 'error ' ];
100
+ $ this ->name = $ uploadedFile ['name ' ];
101
+ $ this ->type = $ uploadedFile ['type ' ];
102
+ $ this ->tmpName = $ uploadedFile ['tmp_name ' ];
103
+ $ this ->size = $ uploadedFile ['size ' ];
104
+ $ this ->error = $ uploadedFile ['error ' ];
104
105
105
106
if (defined ('PATH_STORAGE ' )) {
106
107
$ this ->path = PATH_STORAGE ;
107
108
} else {
108
- $ this ->path = dirname ($ _SERVER [ 'SCRIPT_FILENAME ' ]) . DIRECTORY_SEPARATOR . $ path ;
109
+ $ this ->path = dirname ($ _SERVER ['SCRIPT_FILENAME ' ]) . DIRECTORY_SEPARATOR . $ path ;
109
110
}
110
111
}
111
112
@@ -155,7 +156,7 @@ public function getStream()
155
156
*
156
157
* Sets target filename.
157
158
*
158
- * @param string $name The target filename.
159
+ * @param string $name The target filename.
159
160
* @param string $conversionFunction Conversion function name, by default it's using dash inflector function.
160
161
*
161
162
* @return static
@@ -198,7 +199,7 @@ public function setPath($path = '')
198
199
$ this ->path = PATH_STORAGE . str_replace (PATH_STORAGE , '' , $ path );
199
200
}
200
201
} else {
201
- $ this ->path = dirname ($ _SERVER [ 'SCRIPT_FILENAME ' ]) . DIRECTORY_SEPARATOR . $ path ;
202
+ $ this ->path = dirname ($ _SERVER ['SCRIPT_FILENAME ' ]) . DIRECTORY_SEPARATOR . $ path ;
202
203
}
203
204
}
204
205
@@ -262,17 +263,17 @@ public function store($path = null)
262
263
*/
263
264
public function moveTo ($ targetPath )
264
265
{
265
- if ( ! $ this ->isMoved ) {
266
+ if (! $ this ->isMoved ) {
266
267
$ filename = pathinfo ($ targetPath , PATHINFO_FILENAME );
267
268
$ filename = dash ($ filename );
268
269
269
270
$ fileExtension = pathinfo ($ targetPath , PATHINFO_EXTENSION );
270
271
$ targetPath = pathinfo ($ targetPath , PATHINFO_DIRNAME ) . DIRECTORY_SEPARATOR ;
271
- $ targetPath = str_replace (['\\' ,'/ ' ], DIRECTORY_SEPARATOR , $ targetPath );
272
+ $ targetPath = str_replace (['\\' , '/ ' ], DIRECTORY_SEPARATOR , $ targetPath );
272
273
273
- if ( ! is_file ($ filePath = $ targetPath . $ filename . '. ' . $ fileExtension )) {
274
+ if (! is_file ($ filePath = $ targetPath . $ filename . '. ' . $ fileExtension )) {
274
275
$ targetPath = $ filePath ;
275
- } elseif ( ! is_file ($ filePath = $ targetPath . $ filename . '-1 ' . '. ' . $ fileExtension )) {
276
+ } elseif (! is_file ($ filePath = $ targetPath . $ filename . '-1 ' . '. ' . $ fileExtension )) {
276
277
$ targetPath = $ filePath ;
277
278
} else {
278
279
$ existingFiles = glob ($ targetPath . $ filename . '*. ' . $ fileExtension );
@@ -281,32 +282,34 @@ public function moveTo($targetPath)
281
282
}
282
283
283
284
foreach (range ($ increment + 1 , $ increment + 3 , 1 ) as $ increment ) {
284
- if ( ! is_file ($ filePath = $ targetPath . $ filename . '- ' . $ increment . '. ' . $ fileExtension )) {
285
+ if (! is_file ($ filePath = $ targetPath . $ filename . '- ' . $ increment . '. ' . $ fileExtension )) {
285
286
$ targetPath = $ filePath ;
286
287
break ;
287
288
}
288
289
}
289
290
}
290
291
291
- if ( ! is_writable (dirname ($ targetPath ))) {
292
+ if (! is_writable (dirname ($ targetPath ))) {
292
293
@mkdir (dirname ($ targetPath ), 0777 , true );
293
294
}
294
295
295
296
if (strpos ($ targetPath , ':// ' ) !== false ) {
296
- if ( ! copy ($ this ->tmpName , $ targetPath )) {
297
- throw new \RuntimeException (sprintf ('Cant Move Uploaded File %1 to %2 ' , $ this ->tmpName , $ targetPath ));
297
+ if (!copy ($ this ->tmpName , $ targetPath )) {
298
+ throw new \RuntimeException (sprintf ('Cant Move Uploaded File %1 to %2 ' , $ this ->tmpName ,
299
+ $ targetPath ));
298
300
}
299
301
300
- if ( ! unlink ($ this ->tmpName )) {
302
+ if (! unlink ($ this ->tmpName )) {
301
303
throw new \RuntimeException ('Failed To Remove Uploaded Temp File ' );
302
304
}
303
305
} else {
304
- if ( ! is_uploaded_file ($ this ->tmpName )) {
306
+ if (! is_uploaded_file ($ this ->tmpName )) {
305
307
throw new \RuntimeException ('File Is Not Valid Uploaded File ' );
306
308
}
307
309
308
- if ( ! move_uploaded_file ($ this ->tmpName , $ targetPath )) {
309
- throw new \RuntimeException (sprintf ('Cant Move Uploaded File %1 to %2 ' , $ this ->tmpName , $ targetPath ));
310
+ if (!move_uploaded_file ($ this ->tmpName , $ targetPath )) {
311
+ throw new \RuntimeException (sprintf ('Cant Move Uploaded File %1 to %2 ' , $ this ->tmpName ,
312
+ $ targetPath ));
310
313
}
311
314
}
312
315
@@ -321,7 +324,7 @@ public function moveTo($targetPath)
321
324
322
325
/**
323
326
* UploadFile::getName
324
- *
327
+ *
325
328
* @return string
326
329
*/
327
330
public function getName ()
@@ -333,7 +336,7 @@ public function getName()
333
336
334
337
/**
335
338
* UploadFile::getPath
336
- *
339
+ *
337
340
* @return string
338
341
*/
339
342
public function getPath ()
@@ -437,7 +440,30 @@ public function getClientMediaType()
437
440
*/
438
441
public function getFileMime ()
439
442
{
440
- $ mime = finfo_file (finfo_open (FILEINFO_MIME_TYPE ), $ this ->tmpName );
443
+ $ finfo = finfo_open (FILEINFO_MIME_TYPE ); // return mime type ala mimetype extension
444
+ $ mime = finfo_file ($ finfo , $ this ->tmpName );
445
+
446
+ if (empty ($ mime )) {
447
+ if (function_exists ('mime_content_type ' )) {
448
+ $ mime = mime_content_type ($ this ->name );
449
+ }
450
+ }
451
+
452
+ if (empty ($ mime )) {
453
+ $ mimeTypes = require (PATH_KERNEL . 'Config ' . DIRECTORY_SEPARATOR . 'Mimes.php ' );
454
+
455
+ if (array_key_exists ($ this ->getExtension (), $ mimeTypes )) {
456
+ $ mime = $ mimeTypes [$ this ->getExtension ()];
457
+
458
+ if (is_array ($ mime )) {
459
+ $ mime = reset ($ mimeTypes [$ this ->getExtension ()]);
460
+ }
461
+ }
462
+ }
463
+
464
+ if (empty ($ mime )) {
465
+ $ mime = empty ($ this ->type ) ? 'application/octet-stream ' : $ this ->type ;
466
+ }
441
467
442
468
return $ mime ;
443
469
}
0 commit comments