4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpFoundation\File;
14 use Symfony\Component\HttpFoundation\File\Exception\FileException;
15 use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
16 use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser;
19 * A file uploaded through a form.
21 * @author Bernhard Schussek <bschussek@gmail.com>
22 * @author Florian Eckerstorfer <florian@eckerstorfer.org>
23 * @author Fabien Potencier <fabien@symfony.com>
25 class UploadedFile extends File
28 * Whether the test mode is activated.
30 * Local files are used in test mode hence the code should not enforce HTTP uploads.
34 private $test = false;
37 * The original name of the uploaded file.
41 private $originalName;
44 * The mime type provided by the uploader.
51 * The file size provided by the uploader.
58 * The UPLOAD_ERR_XXX constant provided by the uploader.
65 * Accepts the information of the uploaded file as provided by the PHP global $_FILES.
67 * The file object is only created when the uploaded file is valid (i.e. when the
68 * isValid() method returns true). Otherwise the only methods that could be called
69 * on an UploadedFile instance are:
71 * * getClientOriginalName,
72 * * getClientMimeType,
76 * Calling any other method on an non-valid instance will cause an unpredictable result.
78 * @param string $path The full temporary path to the file
79 * @param string $originalName The original file name
80 * @param string|null $mimeType The type of the file as provided by PHP; null defaults to application/octet-stream
81 * @param int|null $size The file size
82 * @param int|null $error The error constant of the upload (one of PHP's UPLOAD_ERR_XXX constants); null defaults to UPLOAD_ERR_OK
83 * @param bool $test Whether the test mode is active
85 * @throws FileException If file_uploads is disabled
86 * @throws FileNotFoundException If the file does not exist
88 public function __construct($path, $originalName, $mimeType = null, $size = null, $error = null, $test = false)
90 $this->originalName = $this->getName($originalName);
91 $this->mimeType = $mimeType ?: 'application/octet-stream';
93 $this->error = $error ?: UPLOAD_ERR_OK;
94 $this->test = (bool) $test;
96 parent::__construct($path, UPLOAD_ERR_OK === $this->error);
100 * Returns the original file name.
102 * It is extracted from the request from which the file has been uploaded.
103 * Then it should not be considered as a safe value.
105 * @return string|null The original name
107 public function getClientOriginalName()
109 return $this->originalName;
113 * Returns the original file extension.
115 * It is extracted from the original file name that was uploaded.
116 * Then it should not be considered as a safe value.
118 * @return string The extension
120 public function getClientOriginalExtension()
122 return pathinfo($this->originalName, PATHINFO_EXTENSION);
126 * Returns the file mime type.
128 * The client mime type is extracted from the request from which the file
129 * was uploaded, so it should not be considered as a safe value.
131 * For a trusted mime type, use getMimeType() instead (which guesses the mime
132 * type based on the file content).
134 * @return string|null The mime type
138 public function getClientMimeType()
140 return $this->mimeType;
144 * Returns the extension based on the client mime type.
146 * If the mime type is unknown, returns null.
148 * This method uses the mime type as guessed by getClientMimeType()
149 * to guess the file extension. As such, the extension returned
150 * by this method cannot be trusted.
152 * For a trusted extension, use guessExtension() instead (which guesses
153 * the extension based on the guessed mime type for the file).
155 * @return string|null The guessed extension or null if it cannot be guessed
157 * @see guessExtension()
158 * @see getClientMimeType()
160 public function guessClientExtension()
162 $type = $this->getClientMimeType();
163 $guesser = ExtensionGuesser::getInstance();
165 return $guesser->guess($type);
169 * Returns the file size.
171 * It is extracted from the request from which the file has been uploaded.
172 * Then it should not be considered as a safe value.
174 * @return int|null The file size
176 public function getClientSize()
182 * Returns the upload error.
184 * If the upload was successful, the constant UPLOAD_ERR_OK is returned.
185 * Otherwise one of the other UPLOAD_ERR_XXX constants is returned.
187 * @return int The upload error
189 public function getError()
195 * Returns whether the file was uploaded successfully.
197 * @return bool True if the file has been uploaded with HTTP and no error occurred
199 public function isValid()
201 $isOk = $this->error === UPLOAD_ERR_OK;
203 return $this->test ? $isOk : $isOk && is_uploaded_file($this->getPathname());
207 * Moves the file to a new location.
209 * @param string $directory The destination folder
210 * @param string $name The new file name
212 * @return File A File object representing the new file
214 * @throws FileException if, for any reason, the file could not have been moved
216 public function move($directory, $name = null)
218 if ($this->isValid()) {
220 return parent::move($directory, $name);
223 $target = $this->getTargetFile($directory, $name);
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'])));
230 @chmod($target, 0666 & ~umask());
235 throw new FileException($this->getErrorMessage());
239 * Returns the maximum size of an uploaded file as configured in php.ini.
241 * @return int The maximum size of an uploaded file in bytes
243 public static function getMaxFilesize()
245 $iniMax = strtolower(ini_get('upload_max_filesize'));
247 if ('' === $iniMax) {
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);
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;
271 * Returns an informative upload error message.
273 * @return string The error message regarding the specified error code
275 public function getErrorMessage()
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.',
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.';
291 return sprintf($message, $this->getClientOriginalName(), $maxFilesize);