-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUploader.php
141 lines (128 loc) · 3.9 KB
/
Uploader.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
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
<?php
/**
* Odin Uploader.
*
* A single-class PHP library to upload images securely.
*
* PHP support 5.3+
*
* @version 1.0.0
* @author https://github.com/BackendDevops
* @link https://github.com/BackendDevops/uploader
* @license MIT
*/
class Uploader {
/**
* @var array storage for the global array
*/
private $_files = array();
/**
* @var string storage for any errors
*/
private $error = '';
/**
* @param array $_files represents the $_FILES array passed as dependency
*/
/**
* @var string The new image name, to be provided or will be generated
*/
protected $name;
/**
* @var string The image mime type (extension)
*/
protected $mime;
/**
* @var string The full image path (dir + image + mime)
*/
protected $fullPath;
/**
* @var string The folder or image storage location
*/
protected $location;
/**
* @var array The min and max image size allowed for upload (in bytes)
*/
protected $size = array(100, 500000);
public function __construct(array $_files = array())
{
$this->_files=$_files;
$this->mimeTypes=array('image/png','image/jpeg','image/jpeg','image/gif','application/xml','application/pdf','application/vnd.ms-excel');
$this->error="";
}
private function setName(){
if (!$this->name) {
$extention=$this->getExtention();
$this->name = str_replace('.','',uniqid('', true).'_'.str_shuffle(implode(range('e', 'q'))));
$this->name=$this->name.".".$extention;
}
return $this->name;
}
public function getExtention(){
$ex=explode('.',$this->_files['name']);
return end($ex);
}
public function getName(){
if (!$this->name) {
$this->name=$this->setName();
}
return $this->name;
}
public function setLocation($dir){
if (!$this->location) {
$this->location=$dir;
}
return $this->location;
}
private function checkMimeType(){
$typeOfFile=$this->getMimeType();
if(in_array($typeOfFile,$this->mimeTypes)){
$is_valid=true;
}else{
$is_valid=false;
}
return $is_valid;
}
private function getMimeType(){
if (function_exists('mime_content_type')) {
$this->mimeType = mime_content_type($this->_files['tmp_name']);
return $this->mimeType;
}else if (function_exists('finfo_open') ) {
$finfo = finfo_open(FILEINFO_MIME);
$this->mimeType = finfo_file($finfo, $this->_files['tmp_name']);
finfo_close($finfo);
return $this->mimeType;
} else {
$info=getimagesize($this->_files['tmp_name']);
$this->mimeType=$info['mime'];
return $this->mimeType;
}
}
private function isDirectoryValid($dir)
{
return !file_exists($dir) && !is_dir($dir) || is_writable($dir);
}
private function check(){
//step 1
$this->setName();
$location_status=$this->isDirectoryValid($this->location);
$validMime=$this->checkMimeType();
if($location_status && $validMime){
$this->isValid=true;
$this->fullPath=$this->location.$this->name;
}else{
$this->isValid=false;
$this->location_status=$location_status;
$this->validMime=$validMime;
$this->fullPath=$this->location.$this->name;
}
return $this;
}
public function upload(){
if($this->check()->isValid){
return move_uploaded_file($this->_files['tmp_name'], $this->location.$this->name);
}else{
$this->error="Geçersiz Dosya Gönderdiniz";
return $this->error;
}
}
}