-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_tester.php
More file actions
82 lines (75 loc) · 2.25 KB
/
Copy pathfile_tester.php
File metadata and controls
82 lines (75 loc) · 2.25 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
<?php
class MaestroValidate
{
private $files_types = [
'jpg' => 'image/jpg',
'jpeg' => 'image/jpeg',
'png' => 'image/png',
];
public $error_msg = "هذا الإمتداد غير مسموح به";
private function check_if_there_rules($rules)
{
foreach ($rules as $rule) {
if (!isset($this->files_types[$rule])) {
return $rule;
}
}
}
public function add_extension($ext, $mime)
{
$this->files_types[$ext] = $mime;
}
private function return_extension($file)
{
print_r(getimagesize($file)); exit;
$file_name = basename($file);
$ext = pathinfo($file_name, PATHINFO_EXTENSION);
return $ext;
}
private function return_error($error_msg, $extension)
{
return $error_msg . ' ' . $extension;
}
private function type_validation($file, $rules)
{
$mimes_will_search_in = [];
$error;
foreach ($this->files_types as $key => $value) {
foreach ($rules as $rule_key => $rule_value) {
if ($key == $rule_value) {
array_push($mimes_will_search_in, $value);
}
}
}
$file_mime = mime_content_type($file);
if (!in_array($file_mime, $mimes_will_search_in)) {
$ext = $this->return_extension($file);
$error = $this->return_error($this->error_msg, $ext);
}
if (!empty($error)) {
return $error;
}
}
public function validate_file($file, $rules)
{
$rule_error = $this->check_if_there_rules($rules);
if ($rule_error) {
return "<h1>This file type $rule_error not available</h1>";
exit;
} else {
$validate_errors = $this->type_validation($file, $rules);
return $validate_errors;
}
}
}
// file path
$file = $_FILES["file"]["tmp_name"];
$p = new MaestroValidate;
$p->error_msg = "Not Supported";
$p->add_extension('zip', 'application/zip');
$error = $p->validate_file($file, ['png', 'jpg', 'jpeg', 'zip']);
if($error){
echo $error;
}else{
echo "Ok";
}