Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

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

PostFile.php

Zuletzt modifiziert: 09.10.2024, 12:57 - Dateigröße: 3.72 KiB


001  <?php
002  namespace GuzzleHttp\Post;
003   
004  use GuzzleHttp\Mimetypes;
005  use GuzzleHttp\Stream\StreamInterface;
006  use GuzzleHttp\Stream\Stream;
007   
008  /**
009   * Post file upload
010   */
011  class PostFile implements PostFileInterface
012  {
013      private $name;
014      private $filename;
015      private $content;
016      private $headers = [];
017   
018      /**
019       * @param string          $name     Name of the form field
020       * @param mixed           $content  Data to send
021       * @param string|null     $filename Filename content-disposition attribute
022       * @param array           $headers  Array of headers to set on the file
023       *                                  (can override any default headers)
024       * @throws \RuntimeException when filename is not passed or can't be determined
025       */
026      public function __construct(
027          $name,
028          $content,
029          $filename = null,
030          array $headers = []
031      ) {
032          $this->headers = $headers;
033          $this->name = $name;
034          $this->prepareContent($content);
035          $this->prepareFilename($filename);
036          $this->prepareDefaultHeaders();
037      }
038   
039      public function getName()
040      {
041          return $this->name;
042      }
043   
044      public function getFilename()
045      {
046          return $this->filename;
047      }
048   
049      public function getContent()
050      {
051          return $this->content;
052      }
053   
054      public function getHeaders()
055      {
056          return $this->headers;
057      }
058   
059      /**
060       * Prepares the contents of a POST file.
061       *
062       * @param mixed $content Content of the POST file
063       */
064      private function prepareContent($content)
065      {
066          $this->content = $content;
067   
068          if (!($this->content instanceof StreamInterface)) {
069              $this->content = Stream::factory($this->content);
070          } elseif ($this->content instanceof MultipartBody) {
071              if (!$this->hasHeader('Content-Disposition')) {
072                  $disposition = 'form-data; name="' . $this->name .'"';
073                  $this->headers['Content-Disposition'] = $disposition;
074              }
075   
076              if (!$this->hasHeader('Content-Type')) {
077                  $this->headers['Content-Type'] = sprintf(
078                      "multipart/form-data; boundary=%s",
079                      $this->content->getBoundary()
080                  );
081              }
082          }
083      }
084   
085      /**
086       * Applies a file name to the POST file based on various checks.
087       *
088       * @param string|null $filename Filename to apply (or null to guess)
089       */
090      private function prepareFilename($filename)
091      {
092          $this->filename = $filename;
093   
094          if (!$this->filename) {
095              $this->filename = $this->content->getMetadata('uri');
096          }
097   
098          if (!$this->filename || substr($this->filename, 0, 6) === 'php://') {
099              $this->filename = $this->name;
100          }
101      }
102   
103      /**
104       * Applies default Content-Disposition and Content-Type headers if needed.
105       */
106      private function prepareDefaultHeaders()
107      {
108          // Set a default content-disposition header if one was no provided
109          if (!$this->hasHeader('Content-Disposition')) {
110              $this->headers['Content-Disposition'] = sprintf(
111                  'form-data; name="%s"; filename="%s"',
112                  $this->name,
113                  basename($this->filename)
114              );
115          }
116   
117          // Set a default Content-Type if one was not supplied
118          if (!$this->hasHeader('Content-Type')) {
119              $this->headers['Content-Type'] = Mimetypes::getInstance()
120                  ->fromFilename($this->filename) ?: 'text/plain';
121          }
122      }
123   
124      /**
125       * Check if a specific header exists on the POST file by name.
126       *
127       * @param string $name Case-insensitive header to check
128       *
129       * @return bool
130       */
131      private function hasHeader($name)
132      {
133          return isset(array_change_key_case($this->headers)[strtolower($name)]);
134      }
135  }
136