-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.php
60 lines (50 loc) · 1.57 KB
/
upload.php
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
<?php
header('Content-Type: text/plain; charset=utf-8');
/*-------------------------change this----------------*/
$target_dir ='images/';
/*-------------------------------------------------*/
try {
if (
!isset($_FILES['image_file']['error']) ||
is_array($_FILES['image_file']['error'])
) {
throw new RuntimeException('Invalid parameters.');
}
// Check $_FILES['image_file']['error'] value.
switch ($_FILES['image_file']['error']) {
case UPLOAD_ERR_OK:
break;
case UPLOAD_ERR_NO_FILE:
throw new RuntimeException('No file sent.');
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
throw new RuntimeException('Exceeded filesize limit.');
default:
throw new RuntimeException('Unknown errors.');
}
$finfo = new finfo(FILEINFO_MIME_TYPE);
if (false === $ext = array_search(
$finfo->file($_FILES['image_file']['tmp_name']),
array(
'jpg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
),
true
)) {
throw new RuntimeException('Invalid file format.');
}
if (!move_uploaded_file(
$_FILES['image_file']['tmp_name'],
sprintf('./uploads/%s.%s',$target_dir.
sha1_file($_FILES['image_file']['tmp_name']),
$ext
)
)) {
throw new RuntimeException('Failed to move uploaded file.');
}
echo 'File is uploaded successfully.';
} catch (RuntimeException $e) {
echo $e->getMessage();
}
?>