123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- <?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 <bernhard.schussek@symfony.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 = basename($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 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 no error occurred during uploading
- *
- * @api
- */
- public function isValid()
- {
- return $this->error === UPLOAD_ERR_OK;
- }
- /**
- * 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 the file has not been uploaded via Http
- *
- * @api
- */
- public function move($directory, $name = null)
- {
- if ($this->isValid() && ($this->test || is_uploaded_file($this->getPathname()))) {
- return parent::move($directory, $name);
- }
- throw new FileException(sprintf('The file "%s" has not been uploaded via Http', $this->getPathname()));
- }
- /**
- * Returns the maximum size of an uploaded file as configured in php.ini
- *
- * @return type The maximum size of an uploaded file in bytes
- */
- static public function getMaxFilesize()
- {
- $max = trim(ini_get('upload_max_filesize'));
- if ('' === $max) {
- return PHP_INT_MAX;
- }
- switch (strtolower(substr($max, -1))) {
- case 'g':
- $max *= 1024;
- case 'm':
- $max *= 1024;
- case 'k':
- $max *= 1024;
- }
- return (integer) $max;
- }
- }
|