Passed
Branch master (93b6ae)
by refat
12:48
created

UploadeFile::isImage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace System\Http;
4
5
use System\Application;
6
7
class UploadeFile
8
{
9
  /**
10
   * Application Object
11
   *
12
   * @var \System\Application
13
   */
14
  private $app;
15
16
  /**
17
   * File
18
   *
19
   * @var array
20
   */
21
  private $file = [];
22
23
  /**
24
   * File Name
25
   *
26
   * @var string
27
   */
28
  private $fileName;
29
30
  /**
31
   * File Name
32
   *
33
   * @var string
34
   */
35
  private $nameOnly;
36
37
  /**
38
   * File Extension
39
   *
40
   * @var string
41
   */
42
  private $extension;
43
44
  /**
45
   * File Minetype
46
   *
47
   * @var string
48
   */
49
  private $minetype;
50
51
  /**
52
   * File Temp
53
   *
54
   * @var string
55
   */
56
  private $tempFile;
57
58
  /**
59
   * File Size
60
   *
61
   * @var string
62
   */
63
  private $size;
64
65
  /**
66
   * File Error
67
   *
68
   * @var string
69
   */
70
  private $error;
71
72
  public function __construct(Application $app, $input)
73
  {
74
    $this->app = $app;
75
76
    $this->getFileInfo($input);
77
  }
78
79
  private function getFileInfo($input)
80
  {
81
    if (empty($_FILES[$input])) {
82
83
      return;
84
    }
85
86
    $file = $_FILES[$input];
87
88
    $this->error = $file['error'];
89
90
    if ($this->error != UPLOAD_ERR_OK) {
91
92
      return;
93
    }
94
95
    $this->file = $file;
96
97
    $this->fileName = $this->file['name'];
98
99
    $this->minetype = $this->file['type'];
100
101
    $this->size = $this->file['size'];
102
103
    $this->tempFile = $this->file['tmp_name'];
104
105
    $fileInfo = pathinfo($this->fileName);
106
107
    $this->nameOnly = $fileInfo['filename'];
108
109
    $this->extension = $fileInfo['extension'];
110
  }
111
112
  public function exists()
113
  {
114
    return !empty($this->file);
115
  }
116
117
  public function getFileName()
118
  {
119
    return $this->fileName;
120
  }
121
122
  public function getNameOnly()
123
  {
124
    return $this->nameOnly;
125
  }
126
127
  public function getExtension()
128
  {
129
    return $this->extension;
130
  }
131
132
  public function getMinetype()
133
  {
134
    return $this->minetype;
135
  }
136
137
  public function getSize()
138
  {
139
    return $this->size;
140
  }
141
142
  public function getTempFile()
143
  {
144
    return $this->tempFile;
145
  }
146
147
  public function moveTo($target, $newName = null)
148
  {
149
    $newName = $newName ?: sha1(rand()) . sha1(rand());
150
    $newName .= '.' . $this->extension;
151
152
    if (!is_dir($target)) {
153
154
      mkdir($target, 0777, true);
155
    }
156
157
    $filePath = $target . $newName;
158
    $filePath  = rtrim($filePath, '/');
159
    $filePath  = ltrim($filePath, '/');
160
161
    return move_uploaded_file($this->tempFile, $filePath);
162
  }
163
}
164