-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.php
113 lines (109 loc) · 2.29 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
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
<?php
/**
* all right reserved to hobrt
*@author hobrt-programming.com
*@package upload class
*@files upload.php
*/
class up
{
private $error=array(
'1' => 'حجم الملف اكبر من المسموح به',
'2' => 'نوع الملف غير مسموح به',
'3' => 'خطأ في رفع الملف '
);
public $mime ;
public $file_name;
public $max_size;
public $ext;
public $up_folder;
public $upl;
public $url;
public function upload()
{
$err=$_FILES[$this->file_name]['error'];
$name=$_FILES[$this->file_name]['name'];
$type=$_FILES[$this->file_name]['type'];
$size=$_FILES[$this->file_name]['size'];
$tem_name=$_FILES[$this->file_name]["tmp_name"];
if($err>0){
return $this->error['3'];
}
else {
$arr=explode('.', $name);
$ext=end($arr);
if($this->ext_test($ext)){
if($this->mimetype_test($tem_name)){
if($this->size_test($size)) {
$this->url=$this->rname($name);
$test=move_uploaded_file($tem_name, $this->upl.'/'.$this->url);
if($test){
return true;
}
}
else {
return $this->error['1'];
}
}
else {
return $this->error['2'];
}
}
else {
return $this->error['2'];
}
}
}
private function mimetype_test($tmpname){
$finfo = finfo_open( FILEINFO_MIME_TYPE );
$mtype = finfo_file( $finfo, $tmpname );
if(in_array($mtype, $this->mime)){
finfo_close( $finfo );
return TRUE;
}
else {
finfo_close( $finfo );
return FALSE;
}
}
private function ext_test($ext) {
if(in_array($ext, $this->ext)) {
return TRUE;
}
else {
return FALSE;
}
}
private function size_test($size){
if($size > $this->max_size) {
return FALSE;
}
else {
return TRUE;
}
}
private function rname($name) {
$arr=explode('.', $name);
$ext=end($arr);
//$arre=array_pop($arr);
$nm=$this->getkey();
if(file_exists($this->upl.'/'.$nm)) {
return $nm.'_'.time().'.'.$ext;
}
else {
return $nm.".".$ext;
}
}
private function getkey()
{
$alpha = 'ABCDEFGHIJ12KLMNOPQRS67TUVWXY345Zabcdefghijklmn08opqrstuv9wxyz';
global $key ;
$length = 10; // عدد طول النص
for($i=0; $i<$length; $i++){
$ran = rand(0, strlen($alpha)-1);
$key .= substr($alpha, $ran, 1);
}
return $key;
}
}
?>