Open Journal Systems  3.3.0
lib/vendor/symfony/http-foundation/File/UploadedFile.php
1 <?php
2 
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
13 
17 
25 class UploadedFile extends File
26 {
34  private $test = false;
35 
41  private $originalName;
42 
48  private $mimeType;
49 
55  private $size;
56 
62  private $error;
63 
88  public function __construct($path, $originalName, $mimeType = null, $size = null, $error = null, $test = false)
89  {
90  $this->originalName = $this->getName($originalName);
91  $this->mimeType = $mimeType ?: 'application/octet-stream';
92  $this->size = $size;
93  $this->error = $error ?: UPLOAD_ERR_OK;
94  $this->test = (bool) $test;
95 
96  parent::__construct($path, UPLOAD_ERR_OK === $this->error);
97  }
98 
107  public function getClientOriginalName()
108  {
109  return $this->originalName;
110  }
111 
120  public function getClientOriginalExtension()
121  {
122  return pathinfo($this->originalName, PATHINFO_EXTENSION);
123  }
124 
138  public function getClientMimeType()
139  {
140  return $this->mimeType;
141  }
142 
160  public function guessClientExtension()
161  {
162  $type = $this->getClientMimeType();
163  $guesser = ExtensionGuesser::getInstance();
164 
165  return $guesser->guess($type);
166  }
167 
176  public function getClientSize()
177  {
178  return $this->size;
179  }
180 
189  public function getError()
190  {
191  return $this->error;
192  }
193 
199  public function isValid()
200  {
201  $isOk = $this->error === UPLOAD_ERR_OK;
202 
203  return $this->test ? $isOk : $isOk && is_uploaded_file($this->getPathname());
204  }
205 
216  public function move($directory, $name = null)
217  {
218  if ($this->isValid()) {
219  if ($this->test) {
220  return parent::move($directory, $name);
221  }
222 
223  $target = $this->getTargetFile($directory, $name);
224 
225  if (!@move_uploaded_file($this->getPathname(), $target)) {
226  $error = error_get_last();
227  throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error['message'])));
228  }
229 
230  @chmod($target, 0666 & ~umask());
231 
232  return $target;
233  }
234 
235  throw new FileException($this->getErrorMessage());
236  }
237 
243  public static function getMaxFilesize()
244  {
245  $iniMax = strtolower(ini_get('upload_max_filesize'));
246 
247  if ('' === $iniMax) {
248  return PHP_INT_MAX;
249  }
250 
251  $max = ltrim($iniMax, '+');
252  if (0 === strpos($max, '0x')) {
253  $max = intval($max, 16);
254  } elseif (0 === strpos($max, '0')) {
255  $max = intval($max, 8);
256  } else {
257  $max = (int) $max;
258  }
259 
260  switch (substr($iniMax, -1)) {
261  case 't': $max *= 1024;
262  case 'g': $max *= 1024;
263  case 'm': $max *= 1024;
264  case 'k': $max *= 1024;
265  }
266 
267  return $max;
268  }
269 
275  public function getErrorMessage()
276  {
277  static $errors = array(
278  UPLOAD_ERR_INI_SIZE => 'The file "%s" exceeds your upload_max_filesize ini directive (limit is %d KiB).',
279  UPLOAD_ERR_FORM_SIZE => 'The file "%s" exceeds the upload limit defined in your form.',
280  UPLOAD_ERR_PARTIAL => 'The file "%s" was only partially uploaded.',
281  UPLOAD_ERR_NO_FILE => 'No file was uploaded.',
282  UPLOAD_ERR_CANT_WRITE => 'The file "%s" could not be written on disk.',
283  UPLOAD_ERR_NO_TMP_DIR => 'File could not be uploaded: missing temporary directory.',
284  UPLOAD_ERR_EXTENSION => 'File upload was stopped by a PHP extension.',
285  );
286 
287  $errorCode = $this->error;
288  $maxFilesize = $errorCode === UPLOAD_ERR_INI_SIZE ? self::getMaxFilesize() / 1024 : 0;
289  $message = isset($errors[$errorCode]) ? $errors[$errorCode] : 'The file "%s" was not uploaded due to an unknown error.';
290 
291  return sprintf($message, $this->getClientOriginalName(), $maxFilesize);
292  }
293 }
Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser\getInstance
static getInstance()
Definition: lib/vendor/symfony/http-foundation/File/MimeType/ExtensionGuesser.php:50
Symfony\Component\HttpFoundation\File\UploadedFile\getClientOriginalName
getClientOriginalName()
Definition: lib/vendor/symfony/http-foundation/File/UploadedFile.php:122
Symfony\Component\HttpFoundation\File\File\getName
getName($name)
Definition: lib/vendor/symfony/http-foundation/File/File.php:128
Symfony\Component\HttpFoundation\File\File
Definition: lib/vendor/symfony/http-foundation/File/File.php:24
Symfony\Component\HttpFoundation\File
Symfony\Component\HttpFoundation\File\UploadedFile\getError
getError()
Definition: lib/vendor/symfony/http-foundation/File/UploadedFile.php:204
Symfony\Component\HttpFoundation\File\UploadedFile\__construct
__construct($path, $originalName, $mimeType=null, $size=null, $error=null, $test=false)
Definition: lib/vendor/symfony/http-foundation/File/UploadedFile.php:103
Symfony\Component\HttpFoundation\File\UploadedFile\guessClientExtension
guessClientExtension()
Definition: lib/vendor/symfony/http-foundation/File/UploadedFile.php:175
Symfony\Component\HttpFoundation\File\UploadedFile\getClientSize
getClientSize()
Definition: lib/vendor/symfony/http-foundation/File/UploadedFile.php:191
Symfony\Component\HttpFoundation\File\UploadedFile\getErrorMessage
getErrorMessage()
Definition: lib/vendor/symfony/http-foundation/File/UploadedFile.php:290
Symfony\Component\HttpFoundation\File\UploadedFile\move
move($directory, $name=null)
Definition: lib/vendor/symfony/http-foundation/File/UploadedFile.php:231
Symfony\Component\HttpFoundation\File\UploadedFile\getClientOriginalExtension
getClientOriginalExtension()
Definition: lib/vendor/symfony/http-foundation/File/UploadedFile.php:135
Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException
Definition: lib/vendor/symfony/http-foundation/File/Exception/FileNotFoundException.php:19
Symfony\Component\HttpFoundation\File\File\getTargetFile
getTargetFile($directory, $name=null)
Definition: lib/vendor/symfony/http-foundation/File/File.php:106
Symfony\Component\HttpFoundation\File\UploadedFile\getMaxFilesize
static getMaxFilesize()
Definition: lib/vendor/symfony/http-foundation/File/UploadedFile.php:258
Symfony\Component\HttpFoundation\File\UploadedFile
Definition: lib/vendor/symfony/http-foundation/File/UploadedFile.php:25
Symfony\Component\HttpFoundation\File\UploadedFile\isValid
isValid()
Definition: lib/vendor/symfony/http-foundation/File/UploadedFile.php:214
Symfony\Component\HttpFoundation\File\Exception\FileException
Definition: lib/vendor/symfony/http-foundation/File/Exception/FileException.php:19
Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser
Definition: lib/vendor/symfony/http-foundation/File/MimeType/ExtensionGuesser.php:26
Symfony\Component\HttpFoundation\File\UploadedFile\getClientMimeType
getClientMimeType()
Definition: lib/vendor/symfony/http-foundation/File/UploadedFile.php:153