-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreaming-upload.php
More file actions
61 lines (45 loc) · 1.91 KB
/
streaming-upload.php
File metadata and controls
61 lines (45 loc) · 1.91 KB
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
<?php
require __DIR__.'/../vendor/autoload.php';
use Farzai\Transport\Multipart\MultipartBuilderFactory;
use Farzai\Transport\Multipart\StreamingMultipartBuilder;
use Farzai\Transport\TransportBuilder;
echo "Streaming Upload Example\n";
echo "========================\n\n";
// Create a test file
$testFile = sys_get_temp_dir().'/test-upload.txt';
file_put_contents($testFile, str_repeat('Large file content... ', 1000));
$fileSize = filesize($testFile);
echo "Test file: {$testFile}\n";
echo 'File size: '.number_format($fileSize)." bytes\n\n";
// Method 1: Manual streaming builder
echo "Method 1: Manual Streaming Builder\n";
echo "-----------------------------------\n";
$transport = TransportBuilder::make()
->withBaseUri('https://httpbin.org')
->build();
$builder = new StreamingMultipartBuilder;
$stream = $builder
->addFile('file', $testFile, 'upload.txt', 'text/plain')
->addField('description', 'Streaming upload test')
->addField('timestamp', (string) time())
->build();
echo 'Memory before: '.number_format(memory_get_usage(true) / 1024)." KB\n";
$response = $transport->post('/post')
->withBody($stream)
->withHeader('Content-Type', $builder->getContentType())
->send();
echo 'Memory after: '.number_format(memory_get_usage(true) / 1024)." KB\n";
echo 'Status: '.$response->statusCode()."\n\n";
// Method 2: Auto-selection with factory
echo "Method 2: Auto-Selection Factory\n";
echo "---------------------------------\n";
$parts = [
['name' => 'file', 'contents' => $testFile, 'filename' => 'upload.txt'],
['name' => 'description', 'contents' => 'Auto-selected builder'],
];
$builder = MultipartBuilderFactory::create(null, $parts);
echo 'Selected builder: '.($builder instanceof StreamingMultipartBuilder ? 'Streaming' : 'Standard')."\n";
echo 'Threshold: '.MultipartBuilderFactory::getDefaultThreshold()." bytes\n\n";
// Cleanup
unlink($testFile);
echo "Example complete!\n";