/src/Symfony/Component/HttpFoundation/File/UploadedFile.php
PHP | 249 lines | 78 code | 29 blank | 142 comment | 7 complexity | 1cd71c3dd4848cb4363dfc6041c51c35 MD5 | raw file
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\HttpFoundation\File;
- use Symfony\Component\HttpFoundation\File\Exception\FileException;
- use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
- /**
- * A file uploaded through a form.
- *
- * @author Bernhard Schussek <bschussek@gmail.com>
- * @author Florian Eckerstorfer <florian@eckerstorfer.org>
- * @author Fabien Potencier <fabien@symfony.com>
- *
- * @api
- */
- class UploadedFile extends File
- {
- /**
- * Whether the test mode is activated.
- *
- * Local files are used in test mode hence the code should not enforce HTTP uploads.
- *
- * @var Boolean
- */
- private $test = false;
- /**
- * The original name of the uploaded file.
- *
- * @var string
- */
- private $originalName;
- /**
- * The mime type provided by the uploader.
- *
- * @var string
- */
- private $mimeType;
- /**
- * The file size provided by the uploader.
- *
- * @var string
- */
- private $size;
- /**
- * The UPLOAD_ERR_XXX constant provided by the uploader.
- *
- * @var integer
- */
- private $error;
- /**
- * Accepts the information of the uploaded file as provided by the PHP global $_FILES.
- *
- * The file object is only created when the uploaded file is valid (i.e. when the
- * isValid() method returns true). Otherwise the only methods that could be called
- * on an UploadedFile instance are:
- *
- * * getClientOriginalName,
- * * getClientMimeType,
- * * isValid,
- * * getError.
- *
- * Calling any other method on an non-valid instance will cause an unpredictable result.
- *
- * @param string $path The full temporary path to the file
- * @param string $originalName The original file name
- * @param string $mimeType The type of the file as provided by PHP
- * @param integer $size The file size
- * @param integer $error The error constant of the upload (one of PHP's UPLOAD_ERR_XXX constants)
- * @param Boolean $test Whether the test mode is active
- *
- * @throws FileException If file_uploads is disabled
- * @throws FileNotFoundException If the file does not exist
- *
- * @api
- */
- public function __construct($path, $originalName, $mimeType = null, $size = null, $error = null, $test = false)
- {
- if (!ini_get('file_uploads')) {
- throw new FileException(sprintf('Unable to create UploadedFile because "file_uploads" is disabled in your php.ini file (%s)', get_cfg_var('cfg_file_path')));
- }
- $this->originalName = $this->getName($originalName);
- $this->mimeType = $mimeType ?: 'application/octet-stream';
- $this->size = $size;
- $this->error = $error ?: UPLOAD_ERR_OK;
- $this->test = (Boolean) $test;
- parent::__construct($path, UPLOAD_ERR_OK === $this->error);
- }
- /**
- * Returns the original file name.
- *
- * It is extracted from the request from which the file has been uploaded.
- * Then is should not be considered as a safe value.
- *
- * @return string|null The original name
- *
- * @api
- */
- public function getClientOriginalName()
- {
- return $this->originalName;
- }
- /**
- * Returns the original file extension
- *
- * It is extracted from the original file name that was uploaded.
- * Then is should not be considered as a safe value.
- *
- * @return string The extension
- */
- public function getClientOriginalExtension()
- {
- return pathinfo($this->originalName, PATHINFO_EXTENSION);
- }
- /**
- * Returns the file mime type.
- *
- * It is extracted from the request from which the file has been uploaded.
- * Then is should not be considered as a safe value.
- *
- * @return string|null The mime type
- *
- * @api
- */
- public function getClientMimeType()
- {
- return $this->mimeType;
- }
- /**
- * Returns the file size.
- *
- * It is extracted from the request from which the file has been uploaded.
- * Then is should not be considered as a safe value.
- *
- * @return integer|null The file size
- *
- * @api
- */
- public function getClientSize()
- {
- return $this->size;
- }
- /**
- * Returns the upload error.
- *
- * If the upload was successful, the constant UPLOAD_ERR_OK is returned.
- * Otherwise one of the other UPLOAD_ERR_XXX constants is returned.
- *
- * @return integer The upload error
- *
- * @api
- */
- public function getError()
- {
- return $this->error;
- }
- /**
- * Returns whether the file was uploaded successfully.
- *
- * @return Boolean True if the file has been uploaded with HTTP and no error occurred.
- *
- * @api
- */
- public function isValid()
- {
- $isOk = $this->error === UPLOAD_ERR_OK;
- return $this->test ? $isOk : $isOk && is_uploaded_file($this->getPathname());
- }
- /**
- * Moves the file to a new location.
- *
- * @param string $directory The destination folder
- * @param string $name The new file name
- *
- * @return File A File object representing the new file
- *
- * @throws FileException if, for any reason, the file could not have been moved
- *
- * @api
- */
- public function move($directory, $name = null)
- {
- if ($this->isValid()) {
- if ($this->test) {
- return parent::move($directory, $name);
- }
- $target = $this->getTargetFile($directory, $name);
- if (!@move_uploaded_file($this->getPathname(), $target)) {
- $error = error_get_last();
- throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error['message'])));
- }
- @chmod($target, 0666 & ~umask());
- return $target;
- }
- throw new FileException(sprintf('The file "%s" is not valid', $this->getPathname()));
- }
- /**
- * Returns the maximum size of an uploaded file as configured in php.ini
- *
- * @return int The maximum size of an uploaded file in bytes
- */
- public static function getMaxFilesize()
- {
- $max = strtolower(ini_get('upload_max_filesize'));
- if ('' === $max) {
- return PHP_INT_MAX;
- }
- if (preg_match('#^\+?(0x?)?(.*?)([kmg]?)$#', $max, $match)) {
- $shifts = array('' => 0, 'k' => 10, 'm' => 20, 'g' => 30);
- $bases = array('' => 10, '0' => 8, '0x' => 16);
- return intval($match[2], $bases[$match[1]]) << $shifts[$match[3]];
- }
- return 0;
- }
- }