[ Index ] |
PHP Cross Reference of phpBB-3.2.11-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 namespace GuzzleHttp\Post; 3 4 use GuzzleHttp\Mimetypes; 5 use GuzzleHttp\Stream\StreamInterface; 6 use GuzzleHttp\Stream\Stream; 7 8 /** 9 * Post file upload 10 */ 11 class PostFile implements PostFileInterface 12 { 13 private $name; 14 private $filename; 15 private $content; 16 private $headers = []; 17 18 /** 19 * @param string $name Name of the form field 20 * @param mixed $content Data to send 21 * @param string|null $filename Filename content-disposition attribute 22 * @param array $headers Array of headers to set on the file 23 * (can override any default headers) 24 * @throws \RuntimeException when filename is not passed or can't be determined 25 */ 26 public function __construct( 27 $name, 28 $content, 29 $filename = null, 30 array $headers = [] 31 ) { 32 $this->headers = $headers; 33 $this->name = $name; 34 $this->prepareContent($content); 35 $this->prepareFilename($filename); 36 $this->prepareDefaultHeaders(); 37 } 38 39 public function getName() 40 { 41 return $this->name; 42 } 43 44 public function getFilename() 45 { 46 return $this->filename; 47 } 48 49 public function getContent() 50 { 51 return $this->content; 52 } 53 54 public function getHeaders() 55 { 56 return $this->headers; 57 } 58 59 /** 60 * Prepares the contents of a POST file. 61 * 62 * @param mixed $content Content of the POST file 63 */ 64 private function prepareContent($content) 65 { 66 $this->content = $content; 67 68 if (!($this->content instanceof StreamInterface)) { 69 $this->content = Stream::factory($this->content); 70 } elseif ($this->content instanceof MultipartBody) { 71 if (!$this->hasHeader('Content-Disposition')) { 72 $disposition = 'form-data; name="' . $this->name .'"'; 73 $this->headers['Content-Disposition'] = $disposition; 74 } 75 76 if (!$this->hasHeader('Content-Type')) { 77 $this->headers['Content-Type'] = sprintf( 78 "multipart/form-data; boundary=%s", 79 $this->content->getBoundary() 80 ); 81 } 82 } 83 } 84 85 /** 86 * Applies a file name to the POST file based on various checks. 87 * 88 * @param string|null $filename Filename to apply (or null to guess) 89 */ 90 private function prepareFilename($filename) 91 { 92 $this->filename = $filename; 93 94 if (!$this->filename) { 95 $this->filename = $this->content->getMetadata('uri'); 96 } 97 98 if (!$this->filename || substr($this->filename, 0, 6) === 'php://') { 99 $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 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Nov 11 20:33:01 2020 | Cross-referenced by PHPXref 0.7.1 |