GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — xmlvalid-php/src/Multipart.php at main · xmlvalid/xmlvalid-php

Expand file tree

/

Multipart.php

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
80 lines (66 loc) · 2.17 KB
/

Multipart.php

Copy path

File metadata and controls

80 lines (66 loc) · 2.17 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
declare(strict_types=1);
namespace Xmlvalid;
use Xmlvalid\Exception\TransportException;
/**
* A multipart/form-data body, written to php://temp (memory, then a
* temporary file past 2 MB) so large files are copied, not held in memory.
*
* @internal
*/
final class Multipart
{
public readonly string $boundary;
/** @var resource */
private $body;
public function __construct()
{
$this->boundary = 'xmlvalid-' . bin2hex(random_bytes(12));
$body = fopen('php://temp', 'w+b');
if ($body === false) {
throw new TransportException('Cannot open a temporary stream for the request body.');
}
$this->body = $body;
}
public function field(string $name, string $value): self
{
$this->write("--{$this->boundary}\r\nContent-Disposition: form-data; name=\"{$name}\"\r\n\r\n{$value}\r\n");
return $this;
}
/**
* @param string|resource $contents Bytes, or a stream read to its end
*/
public function file(string $name, string $filename, $contents, string $contentType = 'application/octet-stream'): self
{
$filename = str_replace(['"', "\r", "\n"], ['%22', '', ''], $filename);
$this->write("--{$this->boundary}\r\nContent-Disposition: form-data; name=\"{$name}\"; filename=\"{$filename}\"\r\nContent-Type: {$contentType}\r\n\r\n");
if (is_string($contents)) {
$this->write($contents);
} elseif (stream_copy_to_stream($contents, $this->body) === false) {
throw new TransportException("Cannot read {$filename}.");
}
$this->write("\r\n");
return $this;
}
/**
* The finished body, rewound.
*
* @return resource
*/
public function close()
{
$this->write("--{$this->boundary}--\r\n");
rewind($this->body);
return $this->body;
}
public function contentType(): string
{
return "multipart/form-data; boundary={$this->boundary}";
}
private function write(string $bytes): void
{
if (fwrite($this->body, $bytes) === false) {
throw new TransportException('Cannot write the request body.');
}
}
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.