Verzeichnisstruktur phpBB-3.1.0


Veröffentlicht
27.10.2014

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

UploadedFile.php

Zuletzt modifiziert: 09.10.2024, 12:58 - Dateigröße: 9.04 KiB


001  <?php
002   
003  /*
004   * This file is part of the Symfony package.
005   *
006   * (c) Fabien Potencier <fabien@symfony.com>
007   *
008   * For the full copyright and license information, please view the LICENSE
009   * file that was distributed with this source code.
010   */
011   
012  namespace Symfony\Component\HttpFoundation\File;
013   
014  use Symfony\Component\HttpFoundation\File\Exception\FileException;
015  use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
016  use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser;
017   
018  /**
019   * A file uploaded through a form.
020   *
021   * @author Bernhard Schussek <bschussek@gmail.com>
022   * @author Florian Eckerstorfer <florian@eckerstorfer.org>
023   * @author Fabien Potencier <fabien@symfony.com>
024   *
025   * @api
026   */
027  class UploadedFile extends File
028  {
029      /**
030       * Whether the test mode is activated.
031       *
032       * Local files are used in test mode hence the code should not enforce HTTP uploads.
033       *
034       * @var bool
035       */
036      private $test = false;
037   
038      /**
039       * The original name of the uploaded file.
040       *
041       * @var string
042       */
043      private $originalName;
044   
045      /**
046       * The mime type provided by the uploader.
047       *
048       * @var string
049       */
050      private $mimeType;
051   
052      /**
053       * The file size provided by the uploader.
054       *
055       * @var string
056       */
057      private $size;
058   
059      /**
060       * The UPLOAD_ERR_XXX constant provided by the uploader.
061       *
062       * @var int
063       */
064      private $error;
065   
066      /**
067       * Accepts the information of the uploaded file as provided by the PHP global $_FILES.
068       *
069       * The file object is only created when the uploaded file is valid (i.e. when the
070       * isValid() method returns true). Otherwise the only methods that could be called
071       * on an UploadedFile instance are:
072       *
073       *   * getClientOriginalName,
074       *   * getClientMimeType,
075       *   * isValid,
076       *   * getError.
077       *
078       * Calling any other method on an non-valid instance will cause an unpredictable result.
079       *
080       * @param string  $path         The full temporary path to the file
081       * @param string  $originalName The original file name
082       * @param string  $mimeType     The type of the file as provided by PHP
083       * @param int     $size         The file size
084       * @param int     $error        The error constant of the upload (one of PHP's UPLOAD_ERR_XXX constants)
085       * @param bool    $test         Whether the test mode is active
086       *
087       * @throws FileException         If file_uploads is disabled
088       * @throws FileNotFoundException If the file does not exist
089       *
090       * @api
091       */
092      public function __construct($path, $originalName, $mimeType = null, $size = null, $error = null, $test = false)
093      {
094          $this->originalName = $this->getName($originalName);
095          $this->mimeType = $mimeType ?: 'application/octet-stream';
096          $this->size = $size;
097          $this->error = $error ?: UPLOAD_ERR_OK;
098          $this->test = (bool) $test;
099   
100          parent::__construct($path, UPLOAD_ERR_OK === $this->error);
101      }
102   
103      /**
104       * Returns the original file name.
105       *
106       * It is extracted from the request from which the file has been uploaded.
107       * Then it should not be considered as a safe value.
108       *
109       * @return string|null The original name
110       *
111       * @api
112       */
113      public function getClientOriginalName()
114      {
115          return $this->originalName;
116      }
117   
118      /**
119       * Returns the original file extension
120       *
121       * It is extracted from the original file name that was uploaded.
122       * Then it should not be considered as a safe value.
123       *
124       * @return string The extension
125       */
126      public function getClientOriginalExtension()
127      {
128          return pathinfo($this->originalName, PATHINFO_EXTENSION);
129      }
130   
131      /**
132       * Returns the file mime type.
133       *
134       * The client mime type is extracted from the request from which the file
135       * was uploaded, so it should not be considered as a safe value.
136       *
137       * For a trusted mime type, use getMimeType() instead (which guesses the mime
138       * type based on the file content).
139       *
140       * @return string|null The mime type
141       *
142       * @see getMimeType
143       *
144       * @api
145       */
146      public function getClientMimeType()
147      {
148          return $this->mimeType;
149      }
150   
151      /**
152       * Returns the extension based on the client mime type.
153       *
154       * If the mime type is unknown, returns null.
155       *
156       * This method uses the mime type as guessed by getClientMimeType()
157       * to guess the file extension. As such, the extension returned
158       * by this method cannot be trusted.
159       *
160       * For a trusted extension, use guessExtension() instead (which guesses
161       * the extension based on the guessed mime type for the file).
162       *
163       * @return string|null The guessed extension or null if it cannot be guessed
164       *
165       * @see guessExtension()
166       * @see getClientMimeType()
167       */
168      public function guessClientExtension()
169      {
170          $type = $this->getClientMimeType();
171          $guesser = ExtensionGuesser::getInstance();
172   
173          return $guesser->guess($type);
174      }
175   
176      /**
177       * Returns the file size.
178       *
179       * It is extracted from the request from which the file has been uploaded.
180       * Then it should not be considered as a safe value.
181       *
182       * @return int|null     The file size
183       *
184       * @api
185       */
186      public function getClientSize()
187      {
188          return $this->size;
189      }
190   
191      /**
192       * Returns the upload error.
193       *
194       * If the upload was successful, the constant UPLOAD_ERR_OK is returned.
195       * Otherwise one of the other UPLOAD_ERR_XXX constants is returned.
196       *
197       * @return int     The upload error
198       *
199       * @api
200       */
201      public function getError()
202      {
203          return $this->error;
204      }
205   
206      /**
207       * Returns whether the file was uploaded successfully.
208       *
209       * @return bool    True if the file has been uploaded with HTTP and no error occurred.
210       *
211       * @api
212       */
213      public function isValid()
214      {
215          $isOk = $this->error === UPLOAD_ERR_OK;
216   
217          return $this->test ? $isOk : $isOk && is_uploaded_file($this->getPathname());
218      }
219   
220      /**
221       * Moves the file to a new location.
222       *
223       * @param string $directory The destination folder
224       * @param string $name      The new file name
225       *
226       * @return File A File object representing the new file
227       *
228       * @throws FileException if, for any reason, the file could not have been moved
229       *
230       * @api
231       */
232      public function move($directory, $name = null)
233      {
234          if ($this->isValid()) {
235              if ($this->test) {
236                  return parent::move($directory, $name);
237              }
238   
239              $target = $this->getTargetFile($directory, $name);
240   
241              if (!@move_uploaded_file($this->getPathname(), $target)) {
242                  $error = error_get_last();
243                  throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error['message'])));
244              }
245   
246              @chmod($target, 0666 & ~umask());
247   
248              return $target;
249          }
250   
251          throw new FileException($this->getErrorMessage($this->getError()));
252      }
253   
254      /**
255       * Returns the maximum size of an uploaded file as configured in php.ini
256       *
257       * @return int The maximum size of an uploaded file in bytes
258       */
259      public static function getMaxFilesize()
260      {
261          $iniMax = strtolower(ini_get('upload_max_filesize'));
262   
263          if ('' === $iniMax) {
264              return PHP_INT_MAX;
265          }
266   
267          $max = ltrim($iniMax, '+');
268          if (0 === strpos($max, '0x')) {
269              $max = intval($max, 16);
270          } elseif (0 === strpos($max, '0')) {
271              $max = intval($max, 8);
272          } else {
273              $max = intval($max);
274          }
275   
276          switch (substr($iniMax, -1)) {
277              case 't': $max *= 1024;
278              case 'g': $max *= 1024;
279              case 'm': $max *= 1024;
280              case 'k': $max *= 1024;
281          }
282   
283          return $max;
284      }
285   
286      /**
287       * Returns an informative upload error message.
288       *
289       * @param int $errorCode The error code returned by an upload attempt
290       *
291       * @return string The error message regarding the specified error code
292       */
293      private function getErrorMessage($errorCode)
294      {
295          static $errors = array(
296              UPLOAD_ERR_INI_SIZE   => 'The file "%s" exceeds your upload_max_filesize ini directive (limit is %d kb).',
297              UPLOAD_ERR_FORM_SIZE  => 'The file "%s" exceeds the upload limit defined in your form.',
298              UPLOAD_ERR_PARTIAL    => 'The file "%s" was only partially uploaded.',
299              UPLOAD_ERR_NO_FILE    => 'No file was uploaded.',
300              UPLOAD_ERR_CANT_WRITE => 'The file "%s" could not be written on disk.',
301              UPLOAD_ERR_NO_TMP_DIR => 'File could not be uploaded: missing temporary directory.',
302              UPLOAD_ERR_EXTENSION  => 'File upload was stopped by a PHP extension.',
303          );
304   
305          $maxFilesize = $errorCode === UPLOAD_ERR_INI_SIZE ? self::getMaxFilesize() / 1024 : 0;
306          $message = isset($errors[$errorCode]) ? $errors[$errorCode] : 'The file "%s" was not uploaded due to an unknown error.';
307   
308          return sprintf($message, $this->getClientOriginalName(), $maxFilesize);
309      }
310  }
311