-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile-upload.php
More file actions
258 lines (212 loc) · 7.2 KB
/
file-upload.php
File metadata and controls
258 lines (212 loc) · 7.2 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
/**
* File Upload Example
*
* This example demonstrates how to upload files using multipart/form-data
* with the Transport PHP library.
*/
require __DIR__.'/../vendor/autoload.php';
use Farzai\Transport\Multipart\MultipartStreamBuilder;
use Farzai\Transport\TransportBuilder;
// Create a transport instance
$transport = TransportBuilder::make()
->withBaseUri('https://httpbin.org')
->withTimeout(60) // Longer timeout for file uploads
->build();
echo "=== Transport PHP File Upload Examples ===\n\n";
// Example 1: Simple file upload using withFile()
echo "1. Simple File Upload\n";
echo str_repeat('-', 50)."\n";
try {
// Create a temporary test file
$tempFile = tempnam(sys_get_temp_dir(), 'upload_');
file_put_contents($tempFile, 'This is a test file content for upload example.');
$response = $transport->post('/post')
->withFile(
name: 'document',
path: $tempFile,
filename: 'test-document.txt',
additionalFields: [
'description' => 'Test upload',
'category' => 'documents',
]
)
->send();
echo "Status: {$response->statusCode()}\n";
echo "Upload successful!\n";
// Clean up
unlink($tempFile);
echo "\n";
} catch (\Exception $e) {
echo "Error: {$e->getMessage()}\n\n";
}
// Example 2: Multiple file upload with mixed fields
echo "2. Multiple Files with Form Fields\n";
echo str_repeat('-', 50)."\n";
try {
$response = $transport->post('/post')
->withMultipart([
// Text fields
['name' => 'username', 'contents' => 'john_doe'],
['name' => 'email', 'contents' => 'john@example.com'],
// File from contents (string)
[
'name' => 'avatar',
'contents' => 'fake-image-data',
'filename' => 'avatar.jpg',
'content-type' => 'image/jpeg',
],
// Another file
[
'name' => 'document',
'contents' => 'PDF content here',
'filename' => 'report.pdf',
'content-type' => 'application/pdf',
],
])
->send();
echo "Status: {$response->statusCode()}\n";
echo "Multiple files uploaded successfully!\n\n";
} catch (\Exception $e) {
echo "Error: {$e->getMessage()}\n\n";
}
// Example 3: Upload with simple key-value format
echo "3. Simple Format with Text and Files\n";
echo str_repeat('-', 50)."\n";
try {
// Create temporary files
$file1 = tempnam(sys_get_temp_dir(), 'doc1_');
$file2 = tempnam(sys_get_temp_dir(), 'doc2_');
file_put_contents($file1, 'Content of first document');
file_put_contents($file2, 'Content of second document');
$response = $transport->post('/post')
->withMultipart([
// Simple text fields (key => value)
'title' => 'My Documents',
'description' => 'Important files',
// File uploads (array format)
[
'name' => 'file1',
'contents' => file_get_contents($file1),
'filename' => 'document1.txt',
],
[
'name' => 'file2',
'contents' => file_get_contents($file2),
'filename' => 'document2.txt',
],
])
->send();
echo "Status: {$response->statusCode()}\n";
echo "Documents uploaded with metadata!\n";
// Clean up
unlink($file1);
unlink($file2);
echo "\n";
} catch (\Exception $e) {
echo "Error: {$e->getMessage()}\n\n";
}
// Example 4: Advanced - Using MultipartStreamBuilder directly
echo "4. Advanced: Using MultipartStreamBuilder\n";
echo str_repeat('-', 50)."\n";
try {
// Create builder
$builder = new MultipartStreamBuilder;
// Add various fields
$builder->addField('user_id', '12345')
->addField('action', 'upload')
->addFileContents(
name: 'profile_picture',
contents: 'image-binary-data-here',
filename: 'profile.jpg',
contentType: 'image/jpeg'
);
// Create temporary file for another upload
$docFile = tempnam(sys_get_temp_dir(), 'contract_');
file_put_contents($docFile, 'Contract document content');
$builder->addFile(
name: 'contract',
path: $docFile,
filename: 'employment-contract.pdf',
contentType: 'application/pdf'
);
// Use the builder
$response = $transport->post('/post')
->withMultipartBuilder($builder)
->send();
echo "Status: {$response->statusCode()}\n";
echo "Builder boundary: {$builder->getBoundary()}\n";
echo "Total parts: {$builder->count()}\n";
// Clean up
unlink($docFile);
echo "\n";
} catch (\Exception $e) {
echo "Error: {$e->getMessage()}\n\n";
}
// Example 5: Image upload with proper content type
echo "5. Image Upload with Content Type\n";
echo str_repeat('-', 50)."\n";
try {
// Simulate image data
$imageData = 'FAKE_PNG_DATA_'.bin2hex(random_bytes(10));
$response = $transport->post('/post')
->withMultipart([
['name' => 'title', 'contents' => 'My Vacation Photo'],
['name' => 'tags', 'contents' => 'vacation,beach,2024'],
[
'name' => 'photo',
'contents' => $imageData,
'filename' => 'vacation.png',
'content-type' => 'image/png',
],
])
->send();
echo "Status: {$response->statusCode()}\n";
echo "Image uploaded with metadata!\n\n";
} catch (\Exception $e) {
echo "Error: {$e->getMessage()}\n\n";
}
// Example 6: Custom boundary
echo "6. Custom Boundary\n";
echo str_repeat('-', 50)."\n";
try {
$customBoundary = 'MyCustomBoundary123456789';
$response = $transport->post('/post')
->withMultipart(
data: [
'field1' => 'value1',
'field2' => 'value2',
],
boundary: $customBoundary
)
->send();
echo "Status: {$response->statusCode()}\n";
echo "Upload with custom boundary: {$customBoundary}\n\n";
} catch (\Exception $e) {
echo "Error: {$e->getMessage()}\n\n";
}
// Example 7: Large file simulation with progress indication
echo "7. Large File Upload\n";
echo str_repeat('-', 50)."\n";
try {
// Create a larger test file
$largeFile = tempnam(sys_get_temp_dir(), 'large_');
$content = str_repeat('This is a large file content. ', 1000);
file_put_contents($largeFile, $content);
echo 'Uploading file ('.round(strlen($content) / 1024, 2)." KB)...\n";
$startTime = microtime(true);
$response = $transport->post('/post')
->withFile('large_file', $largeFile, 'large-document.txt')
->send();
$duration = round(microtime(true) - $startTime, 2);
echo "Status: {$response->statusCode()}\n";
echo "Upload completed in {$duration} seconds\n";
// Clean up
unlink($largeFile);
echo "\n";
} catch (\Exception $e) {
echo "Error: {$e->getMessage()}\n\n";
}
echo "=== Examples Complete ===\n";
echo "\nNote: These examples use httpbin.org which echoes back the received data.\n";
echo "In production, replace the URL with your actual API endpoint.\n";